Solved

Enumerating through a file system and determining entry types.

Posted on 2014-07-24
3
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I use XML data to populate iOS calendar? 2 497
return confirm and jquery bind 4 673
Apple iOS Starting question 3 309
iPad app accessing MySQL database using PHP 12 155
Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

761 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