如何将所有文件和目录重命名为小写NSLibraryDirectory\myFolder
?
我发现我可以使用:
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:[oldPath lowercaseString] error:&error];
重命名目录。
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:path error:nil];
要获取具有目录内容的数组,但如何检查directoryContent[0]
目录或文件。
你可以使用它来检查NSUrl是目录还是文件:
for (NSURL *item in directoryContent) {
NSString *path = [item path];
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) {
if (isDir) {
NSLog(@"%@ is a directory", path);
} else {
NSLog(@"%@ is a file", path);
}
} else {
NSLog(@"%@ does not exist", path);
}
}
更新:
要重命名目录的所有内容,可以使用以下命令:
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:path error:nil];
BOOL isDir;
for (NSURL *item in directoryContent) {
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) {
if (isDir) {
// create a new directory with lowercase name,
// move contents of old directory to the new one
// then delete the old directory
} else {
[[NSFileManager defaultManager] moveItemAtPath:path toPath:[path lowercaseString] error:&error];
}
}
}
知道文件的isDirectory很好,但是我需要递归地将所有文件和目录重命名为小写
用代码更新了我的答案,以重命名目录的内容