I tried to reproduce your problem by creating directories ~/wtf/FromHere and 
~/wtf/ToHere, with three files in FromHere named 1, 2, and 3. I switch over to 
using "ToHere-NEW" when it detects a file whose name is >= "2".

My test code renamed the directory and switched over to it just fine:

- (void)test
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *fromDir = [@"~/wtf/FromHere" stringByStandardizingPath];
    NSString *originalToDir = [@"~/wtf/ToHere" stringByStandardizingPath];
    NSString *toDir = originalToDir;
    NSError *error = nil;
    NSArray *filesToCopy = [[fileManager contentsOfDirectoryAtPath:fromDir 
error:&error]                    
                            
sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    
    for (NSString *fileName in filesToCopy)
    {
        // Rename the destination directory if necessary.
        if ([fileName intValue] >= 2)
        {    
            NSLog(@"File name is %@; will rename directory if necessary.", 
fileName);
            
            NSString *newToDir = [originalToDir 
stringByAppendingString:@"-NEW"];
            
            if (![fileManager fileExistsAtPath:newToDir])
            {
                if ([fileManager moveItemAtPath:toDir toPath:newToDir 
error:&error])
                {            
                    NSLog(@"...SUCCESS: Renamed %@ to %@.", toDir, newToDir);
                }
                else
                {            
                    NSLog(@"...ERROR: Failed to rename %@ to %@ -- %@.", toDir, 
newToDir, error);
                    abort();
                }
                
                toDir = newToDir;
            }    
            else
            {        
                NSLog(@"...%@ already exists; no need to rename.", newToDir);
            }
        }
        
        // Move the file to the directory at shootPath.
        NSString *oldFilePath = [fromDir 
stringByAppendingPathComponent:fileName];
        NSString *newFilePath = [toDir stringByAppendingPathComponent:fileName];
        
        if ([fileManager copyItemAtPath:oldFilePath toPath:newFilePath 
error:&error])
        {
            NSLog(@"Copied file %@ to %@.", oldFilePath, newFilePath);
        }
        else
        {    
            NSLog(@"...ERROR: Failed to copy %@ to %@ -- %@.", oldFilePath, 
newFilePath, error);
            abort();
        }
    }
}

2011-04-17 01:46:41.013 Scratcho[2630:a0f] Copied file 
/Users/alee/wtf/FromHere/1 to /Users/alee/wtf/ToHere/1.
2011-04-17 01:46:41.014 Scratcho[2630:a0f] File name is 2; will rename 
directory if necessary.
2011-04-17 01:46:41.069 Scratcho[2630:a0f] ...SUCCESS: Renamed 
/Users/alee/wtf/ToHere to /Users/alee/wtf/ToHere-NEW.
2011-04-17 01:46:44.712 Scratcho[2630:a0f] Copied file 
/Users/alee/wtf/FromHere/2 to /Users/alee/wtf/ToHere-NEW/2.
2011-04-17 01:46:44.712 Scratcho[2630:a0f] File name is 3; will rename 
directory if necessary.
2011-04-17 01:46:44.713 Scratcho[2630:a0f] .../Users/alee/wtf/ToHere-NEW 
already exists; no need to rename.
2011-04-17 01:46:46.934 Scratcho[2630:a0f] Copied file 
/Users/alee/wtf/FromHere/3 to /Users/alee/wtf/ToHere-NEW/3.

--Andy

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to