是否有可能在文件夹及其所有子文件夹中搜索某种类型的所有文件(例如 mp4)?到目前为止,我可以使用以下代码一次针对一个文件夹执行此操作:
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);
但我的 cashes 文件夹中有很多子文件夹。我是否必须逐一检查并检查 mp4 文件,或者是否有任何更简单的解决方案?
提前致谢
最佳答案
您可以通过获取所有子路径然后过滤它们来轻松实现此目的。因此,将 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];
关于iOS:在 cashes 文件夹和每个子文件夹中搜索视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14502215/