2011-02-04 62 views
0

我已经做了这个小代码来检查给定目录中有多少个子目录。它只检查第一级,是否有我可以简化?我添加了评论,也许更容易理解我的意图。谢谢!检查Objective-C中目录中项目的属性C

#import <Foundation/Foundation.h>    
      int main (int argc, const char * argv[]){ 
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 



// insert code here... 
NSFileManager *filemgr; 
NSMutableArray *listOfFiles; 
NSDictionary *listOfFolders; 
NSDictionary *controllDir; 
int i, count; 
NSString *myPath; 

filemgr = [NSFileManager defaultManager]; 

myPath = @"/"; 

// list the files in the given directory (myPath) 

listOfFiles = [filemgr directoryContentsAtPath: myPath]; 

// count the number of elements in the array 
count = [listOfFiles count]; 

// check them one by one 
for (i = 0; i < count; i++) 
{ 
    // I need the full path 
    NSString *filePath =[NSString stringWithFormat:@"%@/%@", myPath, [listOfFiles objectAtIndex: i]]; 

    // add every item with its attributes 
    listOfFolders = [filemgr attributesOfItemAtPath:filePath error:NULL]; 

    // to avoid typo get the attribute and create a string 
    controllDir = [filemgr attributesOfItemAtPath:@"/" error:NULL]; 
    NSString *toCheck = [NSString stringWithFormat:@"%@", [controllDir objectForKey:NSFileType]]; 

    // the folder elements one by one 
    NSString *fileType = [NSString stringWithFormat:@"%@", [listOfFolders objectForKey:NSFileType]];  


    if([toCheck isEqualToString:fileType]) 
     { 
      NSLog(@"NAME: %@ TYPE: %@" ,[listOfFiles objectAtIndex:i],[listOfFolders objectForKey:NSFileType]); 
     }     
}    

[pool drain]; 
return 0; 
} 

回答

0

这个怎么样?

NSFileManager *fm = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *subpaths = [fm contentsOfDirectoryAtPath:myPath error:&error]; 
if (!subpaths) ...handle error. 
NSMutableArray *subdirs = [NSMutableArray array]; 
for (NSString *name in subpaths) 
{ 
    NSString *subpath = [myPath stringByAppendingPathComponent:name]; 
    BOOL isDir; 
    if ([fm fileExistsAtPath:subpath isDirectory:&isDir] && 
     isDir) 
    { 
     [subdirs addObject:subpath]; 
    } 
} 

现在,subdirs数组包含所有直接的子目录。

+0

我只关心这一点,如果我检查数组的内容,只给我回数字而不是文件夹的名称。 O做错了什么? – 2011-02-05 01:27:38

1
NSURL *url = [NSURL fileURLWithPath:@"/Users"]; 
    NSError *error; 
    NSArray *items = [[NSFileManager defaultManager] 
         contentsOfDirectoryAtURL:url 
         includingPropertiesForKeys:[NSArray array] 
         options:0 
         error:&error]; 

    NSMutableArray *dirs = [NSMutableArray array]; 
    for (NSURL *url in items) { 
     if (CFURLHasDirectoryPath((CFURLRef)url)) { 
      [dirs addObject:url]; 
     } 
    } 
} 

你可以得到花哨块这样:

NSPredicate *predicate = [NSPredicate predicateWithBlock: 
     ^BOOL (id evaluatedObject, NSDictionary *bindings){ 
      return CFURLHasDirectoryPath((CFURLRef)evaluatedObject); }]; 
NSArray *dirs = [items filteredArrayUsingPredicate:predicate]); 

这里注意到的是,它只击中盘一次,它不花任何时间获取不需要的属性。一旦你构造了NSURL,你总是可以知道它是否是一个目录,因为它以/结尾(这是指定的行为)。这一切都是CFURLHasDirectoryPath()正在做。它实际上并没有碰到磁盘。

1

简要的想法(从手机发布):

  • 使用NSDirectoryEnumerator
  • 它有一个名为fileAttributes的方法,该方法将返回NSDictionary与项目的属性。
  • NSDictionary有一个fileType方法将返回一个常量来指示项目的种类。你可以使用NSFileTypeDirectory作为比较。