首页 > 其他 > 详细

object-c 获得目录(包括子目录)下所有文件

时间:2015-03-19 18:15:12      阅读:403      评论:0      收藏:0      [点我收藏+]

vector<string> getAllFileNamesInDirectory(){

    //ref to: http://stackoverflow.com/questions/5749488/iterating-through-files-in-a-folder-with-nested-folders-cocoa

    NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];

    NSURL *directoryURL = [NSURL URLWithString:@"toolKitRes/model"];   // URL pointing to the directory you want to browse

    NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];

    

    NSDirectoryEnumerator *enumerator = [fileManager

                                         enumeratorAtURL:directoryURL

                                         includingPropertiesForKeys:keys

                                         options:0

                                         errorHandler:^(NSURL *url, NSError *error) {

                                             // Handle the error.

                                             // Return YES if the enumeration should continue after the error.

                                             return YES;

                                         }];

    vector<string> fullPathList;

    for (NSURL *url in enumerator) {

        NSError *error;

        NSNumber *isDirectory = nil;

        if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {

            // handle error

        }

        else if (! [isDirectory boolValue]) {

            // No error and it’s not a directory; do something with the file

            NSString *str_NS=[url absoluteString];

          ////  NSLog(@"%@",str_NS);

            

            string fullPath=[str_NS cStringUsingEncoding:NSASCIIStringEncoding];

            

            fullPathList.push_back(fullPath);

        }

    }

    return fullPathList;

}

object-c 获得目录(包括子目录)下所有文件

原文:http://www.cnblogs.com/wantnon/p/4351059.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!