区分iphone中文件目录中的文件和文件夹

时间:2012-06-09 10:14:14

标签: iphone ios

我通过

获取文档目录中的可用文件和文件夹列表
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager]directoryContentsAtPath:bundleRoot];

我想从“不满”中查看文件和文件夹的列表,我怎样才能获得文件和文件夹的文件和文件?

1 个答案:

答案 0 :(得分:2)

要分隔文件和文件夹,请使用此代码

+ (void) seperateFilesAndFolders
{
    NSString *basePath = [CacheManager pathForCacheFolder];

    NSArray *dirContents = [[NSFileManager defaultManager]directoryContentsAtPath:basePath];

    //This will contains directories
    NSMutableArray *directories = [[NSMutableArray alloc] init];

    //This will contains files
    NSMutableArray *files = [[NSMutableArray alloc] init];

    for (NSString *str in dirContents) 
    {
        NSString *strFilePath = [basePath stringByAppendingPathComponent:str];

        BOOL isDirectory;
        if([[NSFileManager defaultManager] fileExistsAtPath:strFilePath isDirectory:&isDirectory])
        {
            if (isDirectory) {
                [directories addObject:str];
            }
            else {
                [files addObject:str];
            }
        }
    }

}