Rename file in Cocoa?

前端 未结 5 1267
再見小時候
再見小時候 2021-01-01 11:11

How would I rename a file, keeping the file in the same directory?

I have a string containing a full path to a file, and a string containing a the new filename (and

相关标签:
5条回答
  • 2021-01-01 11:49

    here's a more recent example for iOS, the NSFileManager method is a little different:

    NSString *newFilename = [NSString stringWithFormat:@"%@.m4a", newRecording.title];
    
    NSString *newPath = [[newRecording.localPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
    [[NSFileManager defaultManager] moveItemAtPath:newRecording.localPath toPath:newPath error:nil];
    
    0 讨论(0)
  • 2021-01-01 11:51

    NSFileManager and NSWorkspace both have file manipulation methods, but NSFileManager's - (BOOL)movePath:(NSString *)source toPath:(NSString *)destination handler:(id)handler is probably your best bet. Use NSString's path manipulation methods to get the file and folder names right. For example,

    NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
    [[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
    

    Both classes are explained pretty well in the docs, but leave a comment if there's anything you don't understand.

    0 讨论(0)
  • 2021-01-01 11:55

    It's worth noting that moving a file to itself will fail. I had a method that replaced spaces with underscores and made the file name lowercase and renamed the file to the new name. Files with only one word in the name would fail the rename as the new name would be identical on a case-insensitive file system.

    The way I resolved this was to do a two step rename, first renaming the file to a temporary name and then renaming it to the intended name.

    Some pseudocode explaining this:

    NSString *source = @"/FILE.txt";
    NSString *newName = [[source lastPathComponent] lowercaseString];
    NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
    
    [[NSFileManager defaultManager] movePath:source toPath:target error:nil]; // <-- FAILS
    

    The solution:

    NSString *source = @"/FILE.txt";
    NSString *newName = [[source lastPathComponent] lowercaseString];
    
    NSString *temp = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-temp", newName]];
    NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
    
    [[NSFileManager defaultManager] movePath:source toPath:temp error:nil];
    [[NSFileManager defaultManager] movePath:temp toPath:target error:nil];
    
    0 讨论(0)
  • 2021-01-01 11:56

    For the icing on top, a category on NSFileManager:

    @implementation NSFileManager (FileManipulations)
    
    
    - (void)changeFileNamesInDirectory:(NSString *)directory changeBlock:(NSString * (^) (NSString *fileName))block
    {
        NSString *inputDirectory = directory;
    
        NSFileManager *fileManager = [NSFileManager new];
    
        NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:inputDirectory error:nil];
        for (NSString *fileName in fileNames) {
    
            NSString *newFileName =  block(fileName);
    
            NSString *oldPath = [NSString stringWithFormat:@"%@/%@", inputDirectory, oldFileName];
            // move to temp path so case changes can happen
            NSString *tempPath = [NSString stringWithFormat:@"%@-tempName", oldPath];
            NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFileName];
    
            NSError *error = nil;
            [fileManager moveItemAtPath:oldPath toPath:tempPath error:&error];
            if (error) {
                NSLog(@"%@", [error localizedDescription]);
                return;
            }
            [fileManager moveItemAtPath:tempPath toPath:newPath error:&error];
            if (error) {
                NSLog(@"%@", [error localizedDescription]);
            }
        }
    }
    
    
    @end
    
    0 讨论(0)
  • 2021-01-01 12:11

    I just wanted to make this easier to understand for a newbie. Here's all the code:

        NSString *oldPath = @"/Users/brock/Desktop/OriginalFile.png";
    NSString *newFilename = @"NewFileName.png";
    
    NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
    [[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
    
    NSLog( @"File renamed to %@", newFilename );
    
    0 讨论(0)
提交回复
热议问题