Skip to content

Instantly share code, notes, and snippets.

@mikeabdullah
Created October 5, 2012 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeabdullah/3840055 to your computer and use it in GitHub Desktop.
Save mikeabdullah/3840055 to your computer and use it in GitHub Desktop.
Searching for subfolders
- (NSNumber *)directoryContainsASubfolder:(NSURL *)directoryURL error:(NSError **)error;
{
NSArray *contents = [[NSFileManager defaultManager]
contentsOfDirectoryAtURL:directoryURL
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
error:error];
if (!contents) return nil;
for (NSURL *aURL in contents)
{
// Ignore failures for resource values. Such a failure makes it incredibly unlikely
// the URL is a folder we can actually *list*
NSNumber *isDirectory;
if ([aURL getResourceValue:&isDirectory forKey:NSURLIsDirectory error:error] &&
[isDirectory boolValue])
{
// If know it's a directory, but don't know if it's a package, treat as a folder
NSNumber *isPackage;
if (![aURL getResourceValue:&isPackage forKey:NSURLIsPackage error:error] ||
![isPackage boolValue])
{
// Once we find a non-package directory, that's good enough
return @YES;
}
}
}
return @NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment