博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
object-c 获得目录(包括子目录)下所有文件和文件夹路径
阅读量:6941 次
发布时间:2019-06-27

本文共 1712 字,大约阅读时间需要 5 分钟。

void getAllPathNameInDirectory(vector<string>&filePathList,vector<string>&directoryPathList){

    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] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

            NSLog(@"%@",str_NS);

            

            string fullPath=[str_NS cStringUsingEncoding:NSASCIIStringEncoding];

            filePathList.push_back(fullPath);

        }else if([isDirectory boolValue]){

            NSString *str_NS=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

           NSLog(@"%@",str_NS);

            

            string fullPath=[str_NS cStringUsingEncoding:NSASCIIStringEncoding];

            directoryPathList.push_back(fullPath);

        

        }

    }

}

注意:要用

NSString *str_NS=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; (见参考[2])

不能像 参考[1] 中那样写成

NSString *str_NS=[url absoluteString];

否则如果路径中带有空格,则会变成%20。

 

参考[1]: 

参考[2]: http://stackoverflow.com/questions/11262893/nsurl-to-nsstring

转载地址:http://zuinl.baihongyu.com/

你可能感兴趣的文章
荣誉,还是苦逼?| 也议全栈工程师和DevOps
查看>>
gulp详细基础教程
查看>>
CSS基础篇-- position属性讲解
查看>>
Python2.x的编码问题
查看>>
开源编辑器 Atom 简化代码审查过程
查看>>
每秒聚合5亿个指标,Uber 开源大规模指标平台 M3
查看>>
中国航天局向荷兰、德国等移交嫦娥四号载荷数据,并同时发布其他项目合作机会公告 ...
查看>>
Spring MVC原理
查看>>
图灵奖得主长文报告:是什么开启了计算机架构的新黄金十年?(上) ...
查看>>
pseudo tty破除无法自动输入密码的限制
查看>>
阿里云财务软件好会计-好会计财务管理系统介绍 ...
查看>>
推荐:一款分布式的对象存储服务
查看>>
WordPress免费插件的选择指南
查看>>
浮云朝露 2018
查看>>
linux基础命令---dmesg显示内核信息
查看>>
第二十章:异步和文件I/O.(十九)
查看>>
如何用纯 CSS 创作一根闪电连接线
查看>>
Ansible file资源
查看>>
短视频APP源码直播APP源码什么样的好
查看>>
这届百度搜索不太行
查看>>