2013-01-24 117 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