Iterating through files in a folder with nested folders - Cocoa

后端 未结 6 2108
迷失自我
迷失自我 2020-12-07 10:52

I need to access every file in a folder, including file that exist within nested folders. An example folder might look like this.

animals/
 -k.txt
 -d.jpg
 c         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 11:03

    Maybe you can use something like this:

    +(void)openEachFileAt:(NSString*)path
    {
      NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
      for (NSString * file in enumerator)
      {
         // check if it's a directory
         BOOL isDirectory = NO;
        NSString* fullPath = [path stringByAppendingPathComponent:file];
        [[NSFileManager defaultManager] fileExistsAtPath:fullPath
                                             isDirectory: &isDirectory];
        if (!isDirectory)
        {
          // open your file (fullPath)…
        }
        else
        {
          [self openEachFileAt: fullPath];
        }
      }
    }
    

提交回复
热议问题