如何重命名文件,将文件保存在同一目录中?
我有一个包含文件完整路径的字符串,以及一个包含新文件名(没有路径)的字符串,例如:
NSString *old_filepath = @"/Volumes/blah/myfilewithrubbishname.avi";
NSString *new_filename = @"My Correctly Named File.avi";
我知道NSFileManager的movePath:toPath:handler:方法,但我无法研究如何构建新文件的路径..
基本上我正在寻找与以下Python代码等价的东西:
>>> import os
>>> old_filepath = "/Volumes/blah/myfilewithrubbishname.avi"
>>> new_filename = "My Correctly Named File.avi"
>>> dirname = os.path.split(old_filepath)[0]
>>> new_filepath = os.path.join(dirname, new_filename)
>>> print new_filepath
/Volumes/blah/My Correctly Named File.avi
>>> os.rename(old_filepath, new_filepath)
答案 0 :(得分:36)
NSFileManager和NSWorkspace都有文件操作方法,但NSFileManager的- (BOOL)movePath:(NSString *)source toPath:(NSString *)destination handler:(id)handler
可能是你最好的选择。使用NSString的路径操作方法来获取正确的文件和文件夹名称。例如,
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
这两个课程在文档中都有很好的解释,但是如果有任何你不理解的话,请留言。
答案 1 :(得分:14)
值得注意的是,将文件移动到自身会失败。我有一个方法用下划线替换空格,并使文件名小写,并将文件重命名为新名称。名称中只有一个单词的文件将无法重命名,因为新名称在不区分大小写的文件系统上是相同的。
我解决这个问题的方法是进行两步重命名,首先将文件重命名为临时名称,然后将其重命名为预期名称。
一些伪代码解释了这一点:
NSString *source = @"/FILE.txt";
NSString *newName = [[source lastPathComponent] lowercaseString];
NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
[[NSFileManager defaultManager] movePath:source toPath:target error:nil]; // <-- FAILS
解决方案:
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];
答案 2 :(得分:8)
我只是想让新手更容易理解。这是所有代码:
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 );
答案 3 :(得分:4)
这是iOS最近的一个例子,NSFileManager方法有点不同:
NSString *newFilename = [NSString stringWithFormat:@"%@.m4a", newRecording.title];
NSString *newPath = [[newRecording.localPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] moveItemAtPath:newRecording.localPath toPath:newPath error:nil];
答案 4 :(得分:0)
对于锦上添花,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