¿Cómo get la list de directores en NSDocumentDirectory?

He creado algunos directorys en NSDocumentDirectory como

if (![tbxCreateFolder.text isEqualToString:@""]) { NSArray *arrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryStr = [arrPaths objectAtIndex:0]; // Get documents folder NSString *dataPath = [documentsDirectoryStr stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",tbxCreateFolder.text]]; if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){ NSError* error; if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]){ NSLog(@"success"); //Perfrom DB Insertation } else { NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]); NSAssert( FALSE, @"Failed to create directory maybe out of disk space?"); } } 

}

Ahora quiero get la list de directorys que he creado en la carpeta de documentos, pero la respuesta es

 NSArray *arrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *basePathString = ([arrPaths count] > 0) ? [arrPaths objectAtIndex:0] : nil; NSLog(@"dirContents %@",basePathString); 

La respuesta es:

 /Users/amir/Library/Application Support/iPhone Simulator/6.1/Applications/7003E8D5-CCBB-4E54-896B-F4BD2F4D2CB1/Documents 

Aquí está el código:

 NSString *documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // create directory named "test" [[NSFileManager defaultManager] createDirectoryAtPath:[documentDirPath stringByAppendingPathComponent:@"test"] withIntermediateDirectories:YES attributes:nil error:nil]; // retrieved all directories NSLog(@"%@", [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirPath error:nil]); NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirPath error:nil]; BOOL isDir = NO; for (NSString *path in paths) { if ([fileManager fileExistsAtPath:path isDirectory:&isDir] && isDir) { // path is directory } } 

Siempre puede enumerar los contenidos del directory de nivel superior utilizando -fileExistsAtPath:isDirectory: así:

 NSFileManager *shanetworkingFileManager = [NSFileManager defaultManager]; NSDirectoryEnumerator *files = [shanetworkingFileManager enumeratorAtPath:yourPath]; for (NSString *path in files) { BOOL isDirectory = NO; [shanetworkingFileManager fileExistsAtPath:path isDirectory:&isDirectory]; if (isDirectory) { // you found a directory, so do what you will with it } } 

Puede hacer esto con pocas líneas simples de código

 NSString *documentDirectoryPath = // Initialize with Documents path BOOL isDir = NO; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *files = [fileManager contentsOfDirectoryAtPath:documentDirectoryPath error:NULL]; NSMutableArray *directoriesPaths = [NSMutableArray array]; for (NSString *filePath in files) { if ([fileManager fileExistsAtPath:filePath isDirectory:&isDir] && isDir) { [directoriesPaths addObject:filePath]; } } NSLog(@"directories %@", directoriesPaths); 

testing esto,

  NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *strFilePath = [[arrPath objectAtIndex:0] [[NSFileManager defaultManager] createDirectoryAtPath:[strFilePath stringByAppendingPathComponent:@"Sample"] withIntermediateDirectories:YES attributes:nil error:nil];