3

我是 iPhone 新手,

我想检查我的文档目录中有多少个文件夹,我想获取所有文件夹的名称,我想将它存储在我的数组中。

我只想要文件夹的名称,而不想要文件夹内的内容。

任何帮助将不胜感激。

4

6 回答 6

3
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 目录中的条目,它们本身就是目录。)

于 2012-07-30T05:26:30.330 回答
2
    #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 ,该文件是由操作系统创建的。

于 2012-07-30T05:26:17.057 回答
1

做这个:

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);
于 2012-07-30T05:27:20.647 回答
0

看看NSFileManager

听起来你想要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];
于 2012-07-30T05:22:14.903 回答
0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

NSLog(@"files array %@", filePathsArray);
于 2012-07-30T05:31:44.290 回答
0

最简单的方法:

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);
        }
    }
于 2013-11-19T04:09:05.393 回答