I'm working on an app for jailbroken iPhones. I'm trying to get only the directories of an folder. so I'm doing 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 at what I get. when I get the content 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 Preferences", MobileDevice, backups, cache, db, ea, empty, folders, lib, local, lock, log, logs, mobile, msgs, preferences, root, run, spool, stash, tmp, vm )
but then:
2009-07-28 23:23:35.950 Drowser[573:207] apath: Keychains 2009-07-28 23:23:35.954 Drowser[573:207] apath: Managed Preferences 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"! is recognized as folder. how can that be? the others are folders too. I confirmed it via SSH.
does anyone have an idea? Am I doing something wrong?
This is a really easy mistake to make, but it's also really easy to fix. Enumerating the contents of a directory only gives you the name of the item, not the item's full path. You have to build the full path yourself. So where 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 actually have this:
for (NSString *aPath in contentOfFolder) {
NSString * fullPath = [path stringByAppendingPathComponent:aPath];
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir] &&isDir) {
[directoriesOfFolder addObject: fullPath];
}
}