2012-02-28 5 views
2

私はこのメソッドで間違っていることを理解できますか?NSFileManager fileExistsAtPath:isDirectoryの問題

私はディレクトリの内容を再帰的に検出し、それぞれにxmlファイルを作成しようとしています。非再帰的に動作し、適切なxmlファイルを出力します。ディレクトリ検出の再帰的なチョークとaddのすべてのファイル+ディレクトリは "ディレクトリ"要素の下にあります。

_dirArray = [[NSMutableArray alloc] init]; 
_fileArray = [[NSMutableArray alloc] init]; 

NSError *error; 
NSFileManager *filemgr = [NSFileManager defaultManager]; 
NSArray *filelist = [filemgr contentsOfDirectoryAtPath:dirPath error:&error]; 

for (int i = 0; i < filelist.count; i++) 
{ 
    BOOL isDir; 
    NSString *file = [NSString stringWithFormat:@"%@", [filelist objectAtIndex:i]]; 
    [_pathToDirectoryTextField stringValue], [filelist objectAtIndex:i]]; 

    if ([filemgr fileExistsAtPath:dirPath isDirectory:&isDir] && isDir) // I think this is what is crapping out. 
    { 
     [_dirArray addObject:file]; 
    } 
    else 
    { 
     if ([file hasPrefix:@"."]) 
     { 
      // Ignore file. 
     } 
     else 
     { 
      [_fileArray addObject:file]; 
     } 
    } 
} 

ありがとうございました。

答えて

4

私が見ることができる「場合([ファイルマネージャfileExistsAtPath:FONTPATH isDirectory:& isDir] & & isDir)は、」ドキュメントのアップル社の例から来るが、少しずつそれをコピーして、他とそれを使用するようにするだけしない限り、非常に悪い考えですここ

if (itexists and itsadirectory){ 
    //its a existing directory 
    matches directories 
}else{ 
    //it is not a directory or it does not exist 
    matches files that were deleted since you got the listing 
} 

は、私はそれを行うだろうかです::

NSString *dirPath = @"/Volumes/Storage/"; 

NSError *error; 
NSFileManager *filemgr = [NSFileManager defaultManager]; 
NSArray *filelist = [filemgr contentsOfDirectoryAtPath:dirPath error:&error]; 

for (NSString *lastPathComponent in filelist) { 
    if ([lastPathComponent hasPrefix:@"."]) continue; // Ignore file. 
    NSString *fullPath = [dirPath stringByAppendingPathComponent:lastPathComponent]; 
    BOOL isDir; 
    BOOL exists = [filemgr fileExistsAtPath:fullPath isDirectory:&isDir]; 

    if (exists) { 
     if (isDir) { 
      [_dirArray addObject:lastPathComponent];     
     }else{ 
      [_fileArray addObject:lastPathComponent];     
     }      
    } 
} 
+0

実はそれは私が前にそれを持っていた方法で、それはまだ動作していないその意味があるため、ディレクトリや削除されたファイルを取得したいです私のために。しかし、もしあなたがそれが...私の問題はどこか別の場所にあるべきだと確信したら。うーん。 – crewshin

+0

ダイスはありません。しかし、それはいずれかの方法で良い呼び出しです。私はそれを残しておきます。 – crewshin

+0

ああ、彼らはif(exists){if(isDir){}}とは異なるスコープに入る必要があります – valexa

関連する問題