Mr_Fulano
asked on
Enumerating through a file system and determining entry types.
Hi, I have a question regarding how to properly enumerate through a hard drive's file system and identify which artifact are a "Directory" and which are a "File." Below I've included my code for your inspection. I've been able to successfully enumerate through the selected path (which for testing purposes is hard coded), but the part I'm struggling with is how to identify which entry is a Directory and which is simply a regular File.
With this code I get that all the results are "NSFileTypeDirectory", when in fact I know that some are and some aren't. I know I must be missing something very basic - probably, because I'm quite new to Objective-C.
Can someone please shed some light on this matter and determine what it is that I'm doing wrong.
Thank you,
Fulano
With this code I get that all the results are "NSFileTypeDirectory", when in fact I know that some are and some aren't. I know I must be missing something very basic - probably, because I'm quite new to Objective-C.
Can someone please shed some light on this matter and determine what it is that I'm doing wrong.
Thank you,
Fulano
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/Bob/Desktop/FileStorage"; // (Hard coded for testing only)
NSString * nextPath;
NSDirectoryEnumerator * dirEnumerator = [fileManager enumeratorAtPath:path];
NSDictionary * dict = [fileManager attributesOfItemAtPath:path error:nil];
for (nextPath in dirEnumerator) {
NSLog(@"> %@ -- %@", nextPath, [dict valueForKey:@"NSFileType"]);
}
}
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very good thank you!!!
ASKER
BOOL isDirectory = NO;
[fileManager fileExistsAtPath:path isDirectory:&isDirectory];
NSLog(@"name: %@ is %@ directory", path, isDirectory ? @"" : @" not ");