2011-02-04 15 views
0

この小さなコードを使って、指定されたディレクトリにいくつのサブディレクトリがあるかを確認しました。それは最初のレベルだけをチェックします、とにかく簡単にすることができますか?コメントを追加しました。私の意図を理解しやすくなりました。ありがとうございました!この方法だObjective-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アレイにはすべての直下のサブディレクトリが含まれています。ここで注目すべきは

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

はそれだけでディスクを一度に当たるということであり、それは不要な属性を取得する任意の時間を費やすことはありません。

+0

これは私が配列の内容を調べると、フォルダの名前ではなく数字だけが返ってくるということです。どうしたの? –

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]; 
     } 
    } 
} 

あなたは、このようにブロックと空想得ることができます。 NSURLを構築すると、ディレクトリであるかどうかは/で終わるのでいつでも知ることができます(これは特定の動作です)。それはすべてCFURLHasDirectoryPath()さんの行っていることです。実際にはディスクには当たらない。 (携帯電話からの投稿)

1

簡単な思考:

  • 使用NSDirectoryEnumerator
  • fileAttributesというメソッドがあり、アイテムの属性を持つNSDictionaryが返されます。
  • NSDictionaryには、アイテムの種類を示す定数を返すメソッドがあります(fileType)。
  • 比較のために使用できるNSFileTypeDirectoryという素晴らしい定数があります。
関連する問題