Get an array of directories in the document directory

I am trying to return a list of directories present in the application directory. I can get an array containing all files of this extension (for example, .txt or .png files) and I can return all the contents (including directories). The problem occurs when I want to return only directories. Is there an easy way to do this?

Here is my code for returning all .txt files:

- (ViewController *) init { if (self = [super init]) self.title = @"Text Files"; // get the list of .txt files directoryList = [[[[NSFileManager defaultManager] directoryContentsAtPath:DOCUMENTS_FOLDER] pathsMatchingExtensions:[NSArray arrayWithObjects:@".txt", nil]] retain]; NSLog(@"%@", directoryList); return self; } 

and for all files

 - (ViewController *) init { if (self = [super init]) self.title = @"Text Files"; // get the list of all files and directories directoryList = [[[NSFileManager defaultManager] directoryContentsAtPath:DOCUMENTS_FOLDER] retain]; NSLog(@"%@", directoryList); return self; } 
+6
iphone nsfilemanager
source share
2 answers

Try the following:

 - (ViewController *) init { if (self = [super init]) self.title = @"Text Files"; // get the list of all files and directories NSFileManager *fM = [NSFileManager defaultManager]; fileList = [[fM directoryContentsAtPath:DOCUMENTS_FOLDER] retain]; NSMutableArray *directoryList = [[NSMutableArray alloc] init]; for(NSString *file in fileList) { NSString *path = [DOCUMENTS_FOLDER stringByAppendingPathComponent:file]; BOOL isDir = NO; [fM fileExistsAtPath:path isDirectory:(&isDir)]; if(isDir) { [directoryList addObject:file]; } } NSLog(@"%@", directoryList); return self; } 
+22
source share

Mrueg wrote all but one thing that

 fileList = [[fM directoryContentsAtPath:DOCUMENTS_FOLDER]; 

Replaced by

 fileList = [fileManager contentsOfDirectoryAtPath:filePath error:nil]; 
+2
source share

All Articles