【问题标题】:How to get the list of directores in NSDocumentDirectory?如何获取 NSDocumentDirectory 中的目录列表?
【发布时间】:2013-02-02 14:00:50
【问题描述】:

我在 NSDocumentDirectory 中创建了一些目录

if (![tbxCreateFolder.text isEqualToString:@""]) {
NSArray  *arrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryStr = [arrPaths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectoryStr stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",tbxCreateFolder.text]];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){

    NSError* error;
    if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]){

        NSLog(@"success");
        //Perfrom DB Insertation
    }
    else
    {
        NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]);
        NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
    }
}

}

现在我想获取我在文件夹中创建的目录列表,但响应是

NSArray *arrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePathString = ([arrPaths count] > 0) ? [arrPaths objectAtIndex:0] : nil;
NSLog(@"dirContents %@",basePathString);

回复是:

/Users/amir/Library/Application Support/iPhone Simulator/6.1/Applications/7003E8D5-CCBB-4E54-896B-F4BD2F4D2CB1/Documents

【问题讨论】:

    标签: ios objective-c nsfilemanager nsdocumentdirectory


    【解决方案1】:

    代码如下:

    NSString *documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // create directory named "test"
    [[NSFileManager defaultManager] createDirectoryAtPath:[documentDirPath stringByAppendingPathComponent:@"test"] withIntermediateDirectories:YES attributes:nil error:nil];
    // retrieved all directories
    NSLog(@"%@", [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirPath error:nil]);
    
     NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirPath error:nil];
     BOOL isDir = NO;
     for (NSString *path in paths) {
         if ([fileManager fileExistsAtPath:path isDirectory:&isDir] && isDir) {
             // path is directory
         }
    }
    

    【讨论】:

    • 所有文件中的响应也是如此。但我只想获取目录名称。
    【解决方案2】:

    试试这个,

     NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *strFilePath = [[arrPath objectAtIndex:0] 
    [[NSFileManager defaultManager] createDirectoryAtPath:[strFilePath stringByAppendingPathComponent:@"Sample"] withIntermediateDirectories:YES attributes:nil error:nil];
    

    【讨论】:

    • 这会列出文件还是只列出目录?
    【解决方案3】:

    您始终可以像这样使用-fileExistsAtPath:isDirectory: 枚举顶级目录的内容:

    NSFileManager *sharedFileManager = [NSFileManager defaultManager];
    NSDirectoryEnumerator *files = [sharedFileManager enumeratorAtPath:yourPath];
    
    for (NSString *path in files) {
        BOOL isDirectory = NO;
        [sharedFileManager fileExistsAtPath:path isDirectory:&isDirectory];
        if (isDirectory) {
            // you found a directory, so do what you will with it
        }
    }
    

    【讨论】:

      【解决方案4】:

      只需几行简单的代码就可以做到这一点

      NSString *documentDirectoryPath = // Initialize with Documents path
      BOOL isDir = NO;
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSArray *files = [fileManager contentsOfDirectoryAtPath:documentDirectoryPath error:NULL];
      
      NSMutableArray *directoriesPaths = [NSMutableArray array];
      for (NSString *filePath in files) {
          if ([fileManager fileExistsAtPath:filePath isDirectory:&isDir] && isDir) {
              [directoriesPaths addObject:filePath];
          }
      }
      
      NSLog(@"directories %@", directoriesPaths);
      

      【讨论】:

      • 能否请您完成这行“NSString *documentDirectoryPath = // 使用文档路径初始化”
      • 当然。您已经有了 Documents 目录的路径,在您的代码中是 basePathString。在你的下面添加我的代码,但在此之前将 NSString *documentDirectoryPath = // Initialize with Documents path 更改为 NSString *documentDirectoryPath = basePathString
      猜你喜欢
      • 2020-07-24
      • 1970-01-01
      • 2020-09-11
      • 2011-07-15
      • 2020-08-04
      • 2013-11-18
      • 2016-06-19
      • 2012-09-22
      相关资源
      最近更新 更多