0

我通过以下方式获取文档目录中可用文件和文件夹的列表

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager]directoryContentsAtPath:bundleRoot];

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

4

1 回答 1

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];
            }
        }
    }

}
于 2012-06-09T10:25:03.640 回答