Rename file in Cocoa?

前端 未结 5 1291
再見小時候
再見小時候 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 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 );
    

提交回复
热议问题