Link to home
Start Free TrialLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

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

#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;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Fulano

ASKER

Excellent pgnatyuk, this part of your answer was exactly what I needed. The rest of my code worked fine with your suggestion.

BOOL isDirectory = NO;
[fileManager fileExistsAtPath:path isDirectory:&isDirectory];
NSLog(@"name: %@ is %@ directory", path, isDirectory ? @"" : @" not ");
Very good thank you!!!