Macos NSFileManager使用“重命名文件”;moveItemAtURL:“;名称包含路径分隔符,如;foo/bar.extension“;
这感觉很奇怪,我的代码非常简单Macos NSFileManager使用“重命名文件”;moveItemAtURL:“;名称包含路径分隔符,如;foo/bar.extension“;,macos,cocoa,Macos,Cocoa,这感觉很奇怪,我的代码非常简单 // something like "foo/bar" NSString *correctFileName = seriesDict[seriesNumber]; if (correctFileName.length > 0) { // So I'll have a fileName like "foo/bar.extension" which looks like a directory and a file in it... NSStri
// something like "foo/bar"
NSString *correctFileName = seriesDict[seriesNumber];
if (correctFileName.length > 0)
{
// So I'll have a fileName like "foo/bar.extension" which looks like a directory and a file in it...
NSString *pathExtension = [filePath pathExtension];
NSString *correctFilePath = [[[filePath stringByDeletingLastPathComponent]
stringByAppendingPathComponent:correctFileName]
stringByAppendingPathExtension:pathExtension];
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:filePath toPath:correctFilePath error:&error];
// And NSFileManager can not treat it as a legal fileName, kind of expected...
if (error)
{
NSLog(@"Rename file at %@ failed, error: %@", filePath, error);
}
}
似乎可以在Finder中将我的文件重命名为“foo/bar.extension”,如下所示
也许有一种解决方案可以在代码中实现这一点
如果有人能在光线下切碎,我们将不胜感激。Finder中的
/
将转换为:
<代码>/在POSIX样式路径中无效,而:
在HFS样式路径中无效,因此macOS会将这两个字符相互映射
技术上正确的方法是使用cfurlcreateWithfilesystemtobase
创建CFURL
,将kCFURLHFSPathStyle
指定为路径样式,并根据已创建的基本URL进行解析。然后使用cfurlcopyfilesystems
复制完整URL的路径
不过,从实用角度讲,您可以简单地在
/
和之间进行字符串替换:
Finder允许您这样做,但它实际上并没有在文件名中添加目录分隔符,它只是向您显示一个。如果您ls
包含该文件的目录,您将看到真实名称是“foo:bar.mkv”。目录分隔符在文件名中无效。非常感谢,现在我明白了。