2012-01-27 7 views
-1

私はすでにドキュメントディレクトリのパスを取得し、内部にいくつかのディレクトリを作成します。私はすでにディレクトリが存在するかどうかをチェックする方法、それを削除する方法、ファイルを削除する方法を知っていますが、ディレクトリをどのようにリストできますか?ありがとうございました。ファイルのiOSは既存のディレクトリを一覧表示します

私が使用するリスト:

int Count; 
    NSString *path; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"]; 
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL]; 
    for (Count = 0; Count < (int)[directoryContent count]; Count++) 
    { 
     NSLog(@"File %d: %@", (Count + 1), [directoryContent objectAtIndex:Count]); 
    } 

答えて

3

例えば、この方法は、アプリケーションの一時ディレクトリからすべてのファイルを削除します:あなたはNSDirectoryEnumerator *dirEnumeratorを使用し、そのに渡す必要があります見つけることができたよう

- (void)cleatTmpDirectory 
{ 
    // Create a local file manager instance 
    NSFileManager *localFileManager = [[NSFileManager alloc] init]; 
    NSURL *directoryToScan = [NSURL fileURLWithPath:[self applicationTmpDirectory]]; 

    NSDirectoryEnumerator *dirEnumerator = 
    [[localFileManager enumeratorAtURL:directoryToScan 
      includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLIsDirectoryKey,nil] 
           options: NSDirectoryEnumerationSkipsHiddenFiles    | 
     NSDirectoryEnumerationSkipsSubdirectoryDescendants | 
     NSDirectoryEnumerationSkipsPackageDescendants 
          errorHandler:nil] retain]; 

    NSError *error; 
    // Enumerate the dirEnumerator results, each value is stored in allURLs 
    for (NSURL *theURL in dirEnumerator) 
    { 
     // Retrieve whether a directory. 
     NSNumber *isDirectory; 
     [theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL]; 

     if ([isDirectory boolValue] == NO) 
     { 
      [localFileManager removeItemAtURL:theURL error:&error]; 
     } 
    } 

    // Release the localFileManager. 
    [localFileManager release]; 
} 

適切な初期化方法keysを使用します。

1

NSFileManagerの-enumeratorAtPath:メソッドによって返されたNSDirectoryEnumeratorを使用します。

+0

どうすればいいですか? –

+0

@AmiriDev [-enumeratorAtPath: ']のドキュメント(https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html)を見ましたか? %23 // apple_ref/occ/instm/NSFileManager/enumeratorAtPath :)?そこには例があります。 – Caleb

+0

この質問にお答えください。 http://stackoverflow.com/questions/14931224/how-to-get-the-list-of-directores-in-nsdocumentdirectory –

関連する問題