Last Updated: February 25, 2016
·
1.173K
· diegocaridei

List of files in a directory and subdirectories

NSFileManager *fileMgr;
 NSString *entry; 
 NSString *documentsDir;
  NSDirectoryEnumerator *enumerator; 
  BOOL isDirectory; // creo il file manager 
  fileMgr = [NSFileManager defaultManager]; // localizzo la cartella documenti 
  documentsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];   
  // sposto il file manager nella cartella
   documenti =[fileMgr changeCurrentDirectoryPath:documentsDir];   // enumero i documenti contenuti
   enumerator = [fileMgr enumeratorAtPath:documentsDir];  
   // scorro ogni elemento
    while ((entry = [enumerator nextObject]) != nil){   // file o cartella 
      if ([fileMgr fileExistsAtPath:entry isDirectory:&isDirectory] && isDirectory)   
        NSLog (@"Cartella - %@", entry);   
        else     NSLog (@"  File - %@", entry);
         }