浏览Document文件夹下面的所有文件夹和文件列表

    NSFileManager *fileManager = [NSFileManager defaultManager];    
     //在这里获取应用程序Documents文件夹里的文件及文件夹列表    
      
    NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, Y    ES) objectAtIndex:0];   
     
    NSError *error = nil;    
    NSArray *fileList = [[NSArray alloc] init];  
      
     //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组    
    fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];    
      
      //以下这段代码则可以列出给定一个文件夹里的所有子文件夹名    
         
     NSMutableArray *dirArray = [[NSMutableArray alloc] init];  
     NSMutableArray *fileArray = [[NSMutableArray alloc] init];  
     BOOL isDir = NO;    
     //在上面那段程序中获得的fileList中列出文件夹名    
     for (NSString *file in fileList) {    
           NSString *path = [documentDir stringByAppendingPathComponent:file];    
          [fileManager fileExistsAtPath:path isDirectory:(&isDir)];    
         if (isDir) {    
              [dirArray addObject:file];    
         }else{  
               [fileArray addObject:file];  
           }  
         isDir = NO;    
       }    
     NSLog(@"文件夹下面的所有内容:%@",fileList);    
     NSLog(@"所有文件夹:%@",dirArray);   
     NSLog(@"所有文件:%@",fileArray);   
原文地址:https://www.cnblogs.com/appwgh/p/2517576.html