Created
July 24, 2015 19:59
-
-
Save dantheman213/3d4b6644d127d26d8ef5 to your computer and use it in GitHub Desktop.
Recursively list all dirs and files in a iOS app's documents directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL]; | |
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:bundleURL | |
includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] | |
options:NSDirectoryEnumerationSkipsHiddenFiles | |
errorHandler:^BOOL(NSURL *url, NSError *error) | |
{ | |
NSLog(@"[Error] %@ (%@)", error, url); | |
}]; | |
NSMutableArray *mutableFileURLs = [NSMutableArray array]; | |
for (NSURL *fileURL in enumerator) { | |
NSString *filename; | |
[fileURL getResourceValue:&filename forKey:NSURLNameKey error:nil]; | |
NSNumber *isDirectory; | |
[fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil]; | |
// Skip directories with '_' prefix, for example | |
if ([filename hasPrefix:@"_"] && [isDirectory boolValue]) { | |
[enumerator skipDescendants]; | |
continue; | |
} | |
if (![isDirectory boolValue]) { | |
[mutableFileURLs addObject:fileURL]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment