如何在NSLibraryDirectory\myFolder
?
我发现我可以使用:
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:[oldPath lowercaseString] error:&error];
重命名目录。
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:path error:nil];
获取包含目录内容的数组,但我如何检查directoryContent[0]
是否为目录或文件。
答案 0 :(得分:2)
您可以使用它来检查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];
}
}
}
答案 1 :(得分:0)
//Call [self purgeDirectory:__NSLibraryDirectoryPath];
+(void)purgeDirectory:(NSString *)directoryPath __deprecated_msg("For debug purposes only") {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:directoryPath error:&error];
for (NSString *itemPath in directoryContent) {
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:itemPath isDirectory:&isDir]) {
if (isDir) {
[self purgeDirectory:itemPath];//subdirectory
} else {
[[NSFileManager defaultManager] removeItemAtPath:itemPath error:&error];
}
}
}
}