2010-10-14 81 views
0

我想知道目錄中的內容或其文檔或任何其他內容。 如果有一個文件或多個我需要這些文件名。 其實我在創建文檔目錄.. 出口目錄,如果出口是空的話,我從複製主束的zip文件導出文件夾如何查找iPhone中的文檔目錄中的內容

,但下面的代碼無法正常工作。雖然出口是空的,它不會進入,如果塊..有沒有隱藏的文件..?在最後一個循環中它沒有做任何事情。

如何做到這一點。

請幫我

self.fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    self.documentsDir = [paths objectAtIndex:0]; 
    //creating folder 'export' to recieve the file from iTunes 
    NSString *srcFilePath = [NSString stringWithFormat:@"%@/export", self.documentsDir]; 
    [fileManager createDirectoryAtPath:srcFilePath 
      withIntermediateDirectories:NO 
          attributes:nil 
           error:nil]; 
    //copying the zip file into exprort from bundle if export is empty 
    if(![fileManager fileExistsAtPath:srcFilePath]) { 
     NSLog(@"File exists at path: %@", srcFilePath); 
     NSString *resZipfile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"zip" 
                 inDirectory:@"pckg"]; 
     NSLog(@"zip file path ...%@", resZipfile); 
     NSData *mainBundleFile = [NSData dataWithContentsOfFile:resZipfile]; 
     [[NSFileManager defaultManager] createFileAtPath:srcFilePath 
               contents:mainBundleFile 
               attributes:nil];  
    } 
    NSString *eachPath; 
    NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:srcFilePath]; 

    for(eachPath in dirEnum) NSLog(@"FILE: %@", eachPath); 

回答

6
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0]; 

NSFileManager *manager = [[NSFileManager alloc] init]; 
NSDirectoryEnumerator *fileEnumerator = [manager enumeratorAtPath:documentsPath]; 

for (NSString *filename in fileEnumerator) { 
    // Do something with file 
} 

[manager release]; 
+0

不能確定其working.i添加完整的代碼.. – rockey 2010-10-14 20:15:29

+0

'fileExistsAtPath:沒有按'不檢查'export'目錄是否包含任何文件,只檢查它是否存在。由於您在前一行創建它,因此條件始終爲false。 – 2010-10-14 21:35:00

+0

好的......謝謝Can。 – rockey 2010-10-15 11:07:20

0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docsPath = [paths objectAtIndex:0]; 
NSString *srcFilePath = [NSString stringWithFormat:@"%@/export", self.documentsDir]; 
BOOL isDir; 
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:srcFilePath] isDirectory:&isDir]; 
if (!exists && !isDir) { 
    [fileManager createDirectoryAtPath:srcFilePath 
     withIntermediateDirectories:NO 
         attributes:nil 
          error:nil]; 
    NSLog(@"File exists at path: %@", srcFilePath); 
    NSString *resZipfile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"zip" 
                inDirectory:@"pckg"]; 
    NSLog(@"zip file path ...%@", resZipfile); 
    NSData *mainBundleFile = [NSData dataWithContentsOfFile:resZipfile]; 
    [[NSFileManager defaultManager] createFileAtPath:srcFilePath 
              contents:mainBundleFile 
              attributes:nil];  
0

斯威夫特4版本:

guard let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first, 
     let fileEnumerator = fileManager.enumerator(atPath: path) else { 
     return 
    } 
    let fileNames = fileEnumerator.flatMap { $0 as? String } //Here You will have Array<String> with files in current folder and subfolders of your application's Document directory 
+0

添加解釋或答案是沒有真正有用的,否則。 – 2017-12-15 13:41:52

+0

@JaviV我在評論中添加了解釋,但如果您有任何問題,歡迎 – Azon 2017-12-15 13:56:33

相關問題