【问题标题】:How to find the contents in documents directory in iPhone如何在 iPhone 的文档目录中查找内容
【发布时间】:2010-10-14 18:46:18
【问题描述】:

我想知道目录中的内容,无论是其文档还是其他任何内容。 如果有一个或多个文件,我需要这些文件名。 实际上我正在文档目录中创建导出目录.. 如果导出为空,那么我将 zip 文件从主包复制到导出文件夹

但以下代码不起作用。虽然导出是空的,但如果阻止..r 有任何隐藏文件..?并且在最后一个 for 循环中它没有做任何事情。

如何做到这一点。

请帮帮我

    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);

【问题讨论】:

    标签: iphone nsfilemanager


    【解决方案1】:
    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];
    

    【讨论】:

    • fileExistsAtPath: 不检查export 目录是否包含任何文件,它只是检查它是否存在。由于您是在上一行创建的,因此条件始终为 false。
    • 不错,您可以使用不错的 NSFilemanager 类别作为文档路径。在这里我找到了源文件的教程techpaa.com/2012/07/useful-nsfilemanger-category-for.html
    【解决方案2】:
    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];      
    

    【讨论】:

      【解决方案3】:

      Swift 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
      

      【讨论】:

      • 添加解释,否则答案没有用处。
      • @JaviV 我在 cmets 中添加了解释,但如果您有任何问题,欢迎您
      猜你喜欢
      • 1970-01-01
      • 2012-01-11
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多