如何在iPhone中查找文档目录中的内容

时间:2010-10-14 18:46:19

标签: iphone nsfilemanager

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

但以下代码无效。虽然导出是空的,但它不会进入那个如果阻止..还有任何隐藏的文件..?并且在最后一个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);

3 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分: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];      

答案 2 :(得分:0)

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