获取Resources文件夹中的文件列表 - iOS

时间:2011-06-18 21:15:07

标签: iphone ios resources

假设我的iPhone应用程序的“Resources”文件夹中有一个名为“Documents”的文件夹。

有没有办法在运行时获取该文件夹中包含的所有文件的数组或某种类型的列表?

因此,在代码中,它看起来像:

NSMutableArray *myFiles = [...get a list of files in Resources/Documents...];

这可能吗?

7 个答案:

答案 0 :(得分:131)

您可以像这样获取Resources目录的路径,

NSString * resourcePath = [[NSBundle mainBundle] resourcePath];

然后将Documents附加到路径

NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"];

然后,您可以使用NSFileManager

的任何目录列表API
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

注意:将源文件夹添加到捆绑包中时,请确保选择“复制时为任何添加的文件夹创建文件夹参考”

答案 1 :(得分:25)

夫特

更新了Swift 3

let docsPath = Bundle.main.resourcePath! + "/Resources"
let fileManager = FileManager.default

do {
    let docsArray = try fileManager.contentsOfDirectory(atPath: docsPath)
} catch {
    print(error)
}

进一步阅读:

答案 2 :(得分:18)

您也可以尝试以下代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError * error;
NSArray * directoryContents =  [[NSFileManager defaultManager]
                      contentsOfDirectoryAtPath:documentsDirectory error:&error];

NSLog(@"directoryContents ====== %@",directoryContents);

答案 3 :(得分:11)

Swift版本:

    if let files = try? FileManager.default.contentsOfDirectory(atPath: Bundle.main.bundlePath ){
        for file in files {
            print(file)
        }
    }

答案 4 :(得分:6)

列出目录中的所有文件

     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
     NSArray *contents = [fileManager contentsOfDirectoryAtURL:bundleURL
                           includingPropertiesForKeys:@[]
                                              options:NSDirectoryEnumerationSkipsHiddenFiles
                                                error:nil];

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension ENDSWITH '.png'"];
     for (NSString *path in [contents filteredArrayUsingPredicate:predicate]) {
        // Enumerate each .png file in directory
     }

递归枚举目录中的文件

      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
      NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:bundleURL
                                   includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
                                                     options:NSDirectoryEnumerationSkipsHiddenFiles
                                                errorHandler:^BOOL(NSURL *url, NSError *error)
      {
         NSLog(@"[Error] %@ (%@)", error, url);
      }];

      NSMutableArray *mutableFileURLs = [NSMutableArray array];
      for (NSURL *fileURL in enumerator) {
      NSString *filename;
      [fileURL getResourceValue:&filename forKey:NSURLNameKey error:nil];

      NSNumber *isDirectory;
      [fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];

       // Skip directories with '_' prefix, for example
      if ([filename hasPrefix:@"_"] && [isDirectory boolValue]) {
         [enumerator skipDescendants];
         continue;
       }

      if (![isDirectory boolValue]) {
          [mutableFileURLs addObject:fileURL];
       }
     }

有关NSFileManager的更多信息,请here

答案 5 :(得分:4)

Swift 3 (并返回网址)

let url = Bundle.main.resourceURL!
    do {
        let urls = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys:[], options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)
    } catch {
        print(error)
    }

答案 6 :(得分:1)

斯威夫特4:

如果您必须使用子目录"相对于项目" (蓝色文件夹),您可以写:

func getAllPListFrom(_ subdir:String)->[URL]? {
    guard let fURL = Bundle.main.urls(forResourcesWithExtension: "plist", subdirectory: subdir) else { return nil }
    return fURL
}

<强>用法

if let myURLs = getAllPListFrom("myPrivateFolder/Lists") {
   // your code..
}