我是 iPhone 新手,
我想检查我的文档目录中有多少个文件夹,我想获取所有文件夹的名称,我想将它存储在我的数组中。
我只想要文件夹的名称,而不想要文件夹内的内容。
任何帮助将不胜感激。
我是 iPhone 新手,
我想检查我的文档目录中有多少个文件夹,我想获取所有文件夹的名称,我想将它存储在我的数组中。
我只想要文件夹的名称,而不想要文件夹内的内容。
任何帮助将不胜感激。
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *documentsDir [NSSearchPathsForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *allFilesInDocuments = [mgr contentsOfDirectoryAtPath:documentsDir error:NULL];
NSMutableArray *found = [NSMutableArray array];
for (NSString *filename in allFilesInDocuments)
{
NSString *fullPath = [documentsDir stringByAppendingPathComponent:filename];
BOOL isDir;
[mgr fileExistsAtPath:fullPath isDirectory:&isDir];
if (isDir) [found addObject:fullPath]; // or filename
}
现在found
应该包含 Documents 目录的子目录的所有路径/名称(即 Documents 目录中的条目,它们本身就是目录。)
#define rootFileName XXXXXXXX
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourDirectory = [documentsDirectory stringByAppendingPathComponent:rootFileName];
//fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:yourDirectory error:nil];
for(int i = 0; i < [fileList count]; i++)
{
NSString *fileName = [fileList objectAtIndex:i];
NSLog(@"yourFileName:%@",fileName);
if([fileName isEqualToString:@".DS_Store"])
{
NSString *dotDS_StorePath = [NSString stringWithFormat:@"%@/.DS_Store",yourDirectory];
[[NSFileManager defaultManager] removeItemAtPath:dotDS_StorePath error:nil];
}
}
你应该知道一件事,fileLists 有一个更多的文件是 .DS_Store ,该文件是由操作系统创建的。
做这个:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *fileList = [[fileManager directoryContentsAtPath:yourDocumentPath] retain]; // your path here instead (DOCUMENTS_FOLDER)
NSMutableArray *directoryList = [[NSMutableArray alloc] init];
for(NSString *file in fileList) {
NSString *path = [yourDocumentPath stringByAppendingPathComponent:file]; // create path for directory and check it exits or not as directory
BOOL isDir = NO;
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
if(isDir) {
[directoryList addObject:file];
}
}
NSLog(@"%@", directoryList);
听起来你想要subpathsAtPath:
这里的方法是示例代码
BOOL isDir=NO;
NSArray *subpaths;
NSString *fontPath = @"/System/Library/Fonts";
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:fontPath isDirectory:&isDir] && isDir)
subpaths = [fileManager subpathsAtPath:fontPath];
[fileManager release];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(@"files array %@", filePathsArray);
最简单的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (int i = 0; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];
if (![[imageFilenames objectAtIndex:i]isEqualToString:@".DS_Store"])
{
NSLog(@"\n %@",imageName);
}
}