2013-01-24 118 views
0

是否有可能搜索某个文件夹及其所有子文件夹中某种类型的所有文件(例如mp4)?现在我能够以此为一个文件夹,在使用此代码时:iOS:搜索cashes文件夹和每个子文件夹中的视频文件

NSFileManager *fm = [NSFileManager defaultManager]; 
NSArray *dirContents = [fm contentsOfDirectoryAtPath:cachesPath error:nil]; 
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp4'"]; 
NSArray *filesWithAttr = [dirContents filteredArrayUsingPredicate:fltr]; 

NSLog(@"%@", filesWithAttr); 

但我有我的现金文件夹中有很多子文件夹。我是否必须通过其中的每一个并检查mp4文件还是有更简单的解决方案?

在此先感谢

+1

看一看'[FM fileExistsAtPath:yourPath isDirectory:&foo];'和'[FM subpathsAtPath:yourPath];我需要' – xapslock

+0

究竟是什么。非常感谢 – Bautzi89

回答

3

你可以得到所有的子路径,然后过滤他们很容易做到这一点。因此,请将contentsOfDirectoryAtPath替换为subpathsOfDirectoryAtPath,它应该可以工作。该函数执行深层枚举,因此所有子目录也包含在内。

NSFileManager *fm = [NSFileManager defaultManager]; 
NSArray *dirContents = [fm subpathsOfDirectoryAtPath:cachesPath error:nil]; 
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp4'"]; 
NSArray *filesWithAttr = [dirContents filteredArrayUsingPredicate:fltr]; 

功能描述: Apple iOS Developer Library