Solved

Enumerating through a file system and determining entry types.

Posted on 2014-07-24
3
191 Views
Last Modified: 2016-02-29
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
Comment
Question by:Mr_Fulano
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 40220859
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
 

Author Comment

by:Mr_Fulano
ID: 40233452
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
 

Author Closing Comment

by:Mr_Fulano
ID: 40233453
Very good thank you!!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now