IPhone: NSFilemanager fileExistsAtPath: isDirectory: not working properly?

I am working on an application for jailbroken iPhones. I am trying to get only folder directories. so i do this:

NSArray *contentOfFolder = [[NSFileManager defaultManager] directoryContentsAtPath:path]; NSLog(@"contentOfFolder: %@", contentOfFolder); directoriesOfFolder = [[NSMutableArray alloc] initWithCapacity:100]; for (NSString *aPath in contentOfFolder) { NSLog(@"apath: %@", aPath); BOOL isDir; if ([[NSFileManager defaultManager] fileExistsAtPath:aPath isDirectory:&isDir] &&isDir) { [directoriesOfFolder addObject:aPath]; NSLog(@"directoriesOfFolder %@", directoriesOfFolder); } } NSLog(@"dirctories %@", directoriesOfFolder); 

but look what I get. when i get the contents of the folder everything looks fine:

2009-07-28 23: 23: 35.930 Drowser [573: 207] new path / private / var 2009-07-28 23: 23: 35.945 Drowser [573: 207] contentOfFolder: (Keychains, "Managed Settings", Mobile device , backup, cache, db, ea, empty, folders, PB local, lock, log, logs, mobile, msg, preferences, root, run, spool, hangout, TMP, V.M.)

but then:

2009-07-28 23: 35: 95.9 Drowser [573: 207] apath: Keychains 2009-07-28 23: 23: 35,954 Drowser [573: 207] apath: Managed Settings 2009-07-28 23: 23: 35.959 Drowser [573: 207] apath: MobileDevice 2009-07-28 23: 23: 35.984 Drowser [573: 207] apath: backups 2009-07-28 23: 23: 35.993 Drowser [573: 207] apath: cache 2009-07- 28 23: 23: 36.002 Drowser [573: 207] apath: db 2009-07-28 23: 23: 36.011 Drowser [573: 207] apath: ea 2009-07-28 23: 23: 36.019 Drowser [573: 207] apath: empty 2009-07-28 23: 23: 36.028 Drowser [573: 207] apath: folders 2009-07-28 23: 23: 36.037 Drowser [573: 207] apath: lib 2009-07-28 23:23: 36.046 Drowser [573: 207] directoriesOfFolder (Lib)

only "lib"! recognized as a folder. how can it be? others are folders too. I confirmed this through SSH.

Anyone have an idea? Am I doing something wrong?

+7
objective-c iphone nsfilemanager
source share
1 answer

This is a very simple mistake, but it is also very easy to fix. Enumerating the contents of a directory gives only the name of the element, not the full path to the element. You must build the full path yourself. So where do you have:

 for (NSString *aPath in contentOfFolder) { NSLog(@"apath: %@", aPath); BOOL isDir; if ([[NSFileManager defaultManager] fileExistsAtPath:aPath isDirectory:&isDir] &&isDir) { [directoriesOfFolder addObject:aPath]; NSLog(@"directoriesOfFolder %@", directoriesOfFolder); } } 

You should have the following:

 for (NSString *aPath in contentOfFolder) { NSString * fullPath = [path stringByAppendingPathComponent:aPath]; BOOL isDir; if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir] &&isDir) { [directoriesOfFolder addObject: fullPath]; } } 
+23
source share

All Articles