1

I am able to list the content inside Document Directory in Table view. But i have 50 folders that i have downloaded from various sources in document directory. Now i want fetch all the 50 folders content along with document directory content...

//This is my Code...
NSArray * paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError * error;
NSArray *extensions = [NSArray arrayWithObjects:@"mp4", @"folder", @"mov", @"m4v", @"pdf", @"doc", @"xls", @"mp3" ,nil];
directoryContents =  [[NSFileManager defaultManager]
                      contentsOfDirectoryAtPath:documentsDirectory error:&error];
directoryContents = [directoryContents filteredArrayUsingPredicate: 
                      [NSPredicate predicateWithFormat:@"pathExtension IN %@", extensions ]];
NSLog(@"directoryContents ====== %@",directoryContents);
4

1 回答 1

3
test=[[NSMutableArray alloc]init];// allocate mutable array Globally
 NSArray * paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];

NSDirectoryEnumerator *de = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory] ;

NSString *file;
while ((file = [de nextObject]))

 if (([[file pathExtension] isEqualToString:@"mp3"])  ||  ([[file pathExtension]        isEqualToString:@"mp4"]) ||  ([[file pathExtension] isEqualToString:@"mov"]) || ([[file pathExtension] isEqualToString:@"avi"]) || ([[file pathExtension] isEqualToString:@"mkv"]))

    [test addObject:file.lastPathComponent];
    NSLog(@"%@",test);

// This will list only all content inside folder or subfolder but not the folder. Just you have to pass the string matching to your extension in if condition

于 2013-07-10T06:16:47.667 回答