iPhone中我的目录路径中的文件夹名称

时间:2012-07-30 05:15:34

标签: iphone ios ipad nsfilemanager nsdocumentdirectory

我是iPhone新手,

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

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

我们将不胜感激。

6 个答案:

答案 0 :(得分: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目录中的目录本身的条目。)

答案 1 :(得分: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,该文件是由os创建的。

答案 2 :(得分: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);

答案 3 :(得分: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];

答案 4 :(得分:0)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

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

NSLog(@"files array %@", filePathsArray);

答案 5 :(得分: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);
        }
    }