繁体   English   中英

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

[英]How to find the contents in documents directory in iPhone

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

但以下代码不起作用。 尽管导出为空,但如果block..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);
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];

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM