从目录中检索文件

时间:2014-09-09 20:55:40

标签: ios xcode5

我的应用程序使用不同的名称格式在Documentary中创建Png,Pdf和Jpg文件和存储。我如何一次检索所有文件类型。例如,如果我想计算纪录片中有多少个pdf文件,所有文件都存在,我只需要计算pdf文件。如何为特定文件类型实现此目的。

   NSFileManager *filemanage = [NSFileManager defaultManager];
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *filelist= [filemanage contentsOfDirectoryAtPath:[docPaths objectAtIndex:0] error:nil];

NSInteger filescount = [filelist count];
NSString *filesnumber = [NSString stringWithFormat:@"%d", filescount];
NSLog(@"filesnumber:%@", filesnumber);
fileLabel.text = filesnumber;

2 个答案:

答案 0 :(得分:0)

NSFileManager *filemanage = [NSFileManager defaultManager];
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *filelist= [filemanage contentsOfDirectoryAtPath:[docPaths objectAtIndex:0] error:nil];

NSInteger filescount = 0;
for (NSString *fileName in filelist) {
    if ([[fileName pathExtension] isEqualToString:@"pdf"]) {
        filescount++;
    }
}

NSString *filesnumber = [NSString stringWithFormat:@"%d", filescount];
NSLog(@"filesnumber:%@", filesnumber);
fileLabel.text = filesnumber;

你可以||添加.jpg,.png和其他任何你想要的if语句。

答案 1 :(得分:0)

NSFileManager *filemanage = [NSFileManager defaultManager];
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *filelist = [filemanage contentsOfDirectoryAtPath:[docPaths objectAtIndex:0] error:nil];

NSInteger fileCountPDF = 0;
for (NSString *fileName in filelist) {
    NSString *extension = fileName.pathExtension;

    if ([extension caseInsensitiveCompare:@"pdf"] == NSOrderedSame) {
        NSLog(@"PDF file found");
        fileCountPDF++;
    }
}

NSLog(@"PDF files: %d",fileCountPDF);

//...