文件管理之---遍历Documents下的所有文件以及文件夹

NSString *documentPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).lastObject;

NSLog(@"111======%@", documentPath);


NSString *docsDir = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];

    

NSFileManager *fileManager = [NSFileManagerdefaultManager];

    

NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:documentPath];

    

NSString *fileName;    

    

while (fileName = [dirEnum nextObject]) {

        

   NSLog(@"----------FielName : %@" , fileName);

   NSLog(@"-----------------FileFullPath : %@" , [docsDirstringByAppendingPathComponent:fileName]) ;

}


注意:directoryContentsAtPath:弃用了 用这个-contentsOfDirectoryAtPath:error:方法代替

需要获得目录的内容列表,使用enumeratorAtPath:方法或者directoryContentsAtPath:方法,可以完成枚举过程。

如果使用第一种enumeratorAtPath:方法,一次可以枚举指定目录中的每个文件。默认情况下,如果其中一个文件为目录,那么也会递归枚举它的内容。在这个过程中,通过向枚举对象发送一条skipDescendants消息,可以动态地阻止递归过程,从而不再枚举目录中的内容。

对于directoryContentsAtPath:方法,使用这个方法,可以枚举指定目录的内容,并在一个数组中返回文件列表。如果这个目录中的任何文件本身是个目录,这个方法并不递归枚举它的内容。

代码如下:

[cpp]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. int main(int argc, const char * argv[])  
  4. {  
  5.   
  6.     @autoreleasepool {  
  7.           
  8.         NSString *path;  
  9.         NSFileManager *fm;  
  10.         NSDirectoryEnumerator *dirEnum;  
  11.         NSArray *dirArray;  
  12.           
  13.         fm = [NSFileManager defaultManager];  
  14.           
  15.         //获取当前的工作目录的路径  
  16.         path = [fm currentDirectoryPath];  
  17.           
  18.         //遍历这个目录的第一种方法:(深度遍历,会递归枚举它的内容)  
  19.         dirEnum = [fm enumeratorAtPath:path];  
  20.           
  21.         NSLog(@"1.Contents of %@:",path);  
  22.         while ((path = [dirEnum nextObject]) != nil)  
  23.         {  
  24.             NSLog(@"%@",path);  
  25.         }  
  26.           
  27.         //遍历目录的另一种方法:(不递归枚举文件夹种的内容)  
  28.         dirArray = [fm directoryContentsAtPath:[fm currentDirectoryPath]];  
  29.         NSLog(@"2.Contents using directoryContentsAtPath:");  
  30.           
  31.         for(path in dirArray)  
  32.             NSLog(@"%@",path);     
  33.           
  34.     }  
  35.     return 0;  
  36. }  


如果对上述代码while循环做如下修改,可以阻止任何子目录中的枚举:

[cpp]  view plain  copy
  1. while ((path = [dirEnum nextObject]) != nil)  
  2. {  
  3.       
  4.     NSLog(@"%@",path);  
  5.       
  6.     BOOL flag;  
  7.     [fm fileExistsAtPath:path isDirectory:&flag];  
  8.     if(flag == YES)  
  9.         [dirEnum skipDescendants];  
  10. }  


这里flag是一个BOOL类型的变量。如果指定的路径是目录,则fileExistsAtPath:在flag中存储YES,否则存储NO。

另外提醒下,无须像在这个程序中那样进行快速枚举,使用以下NSLog调用也可以显示所有的dirArray的内容:

[cpp]  view plain  copy
  1. NSLog(@"%@",dirArray);  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值