Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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

0
Mr_Fulano
Asked:
Mr_Fulano
  • 2
1 Solution
 
pgnatyukCommented:
The following code will detect:
Add:
BOOL isDirectory = NO;
[fileManager fileExistsAtPath:path isDirectory:&isDirectory];
NSLog(@"name: %@ is %@ directory", path, isDirectory ? @"" : @" not ");

Open in new window

If you need an enumerator:
#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)
        BOOL isDirectory = NO;
        [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
        NSLog(@"name: %@ is %@ directory", path, isDirectory ? @"" : @" not ");

        NSDirectoryEnumerator *dirEnumerator = [fileManager enumeratorAtURL:[NSURL URLWithString:path]
                                        includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
                                                           options:NSDirectoryEnumerationSkipsHiddenFiles | NSDirectoryEnumerationSkipsSubdirectoryDescendants
                                                      errorHandler:nil];
        
        for (NSURL *name in dirEnumerator) {
            NSNumber *isDirectory;
            [name getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
            NSLog(@"name: %@ is %@ directory", name.absoluteString, [isDirectory boolValue] ? @"" : @" not ");
        }
    }
    return 0;
}

Open in new window

0
 
Mr_FulanoAuthor Commented:
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 ");
0
 
Mr_FulanoAuthor Commented:
Very good thank you!!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now