文件search与特定的扩展目标c

我正在处理iPhone项目中的一些文件操作。 在哪里我需要search特定扩展名的文件。 一种select是手动处理每个文件和目录来查找。 我的问题是,有没有简单的方法来做到这一点?

谢谢

看到使用NSFileManager你可以得到的文件和波纹pipe的条件,你可以得到具有特定扩展名的文件,其工作在文件目录..

 -(NSArray *)findFiles:(NSString *)extension { NSMutableArray *matches = [[NSMutableArray alloc]init]; NSFileManager *manager = [NSFileManager defaultManager]; NSString *item; NSArray *contents = [manager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil]; for (item in contents) { if ([[item pathExtension]isEqualToString:extension]) { [matches addObject:item]; } } return matches; } 

与您的search文件使用此数组..获取NSArraytypes的返回,所以使用NSArray对象来存储此数据…

我希望这对你有帮助…

我还没有find任何我能说的事情,很简单,最后我必须写我自己的代码来做到这一点。 我在这里张贴这个,因为也许有人find这个帮助完整。

 -(void)search{ @autoreleasepool { NSString *baseDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSFileManager *defFM = [NSFileManager defaultManager]; BOOL isDir = YES; NSArray *fileTypes = [[NSArray alloc] initWithObjects:@"mp3",@"mp4",@"avi",nil]; NSMutableArray *mediaFiles = [self searchfiles:baseDir ofTypes:fileTypes]; NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [docDir stringByAppendingPathComponent:@"playlist.plist"]; if(![defFM fileExistsAtPath:filePath isDirectory:&isDir]){ [defFM createFileAtPath:filePath contents:nil attributes:nil]; } NSMutableDictionary *playlistDict = [[NSMutableDictionary alloc]init]; for(NSString *path in mediaFiles){ NSLog(@"%@",path); [playlistDict setValue:[NSNumber numberWithBool:YES] forKey:path]; } [playlistDict writeToFile:filePath atomically:YES]; [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshplaylist" object:nil]; } } 

现在recursion的方法

 -(NSMutableArray*)searchfiles:(NSString*)basePath ofTypes:(NSArray*)fileTypes{ NSMutableArray *files = [[[NSMutableArray alloc]init] autorelease]; NSFileManager *defFM = [NSFileManager defaultManager]; NSError *error = nil; NSArray *dirPath = [defFM contentsOfDirectoryAtPath:basePath error:&error]; for(NSString *path in dirPath){ BOOL isDir; path = [basePath stringByAppendingPathComponent:path]; if([defFM fileExistsAtPath:path isDirectory:&isDir] && isDir){ [files addObjectsFromArray:[self searchfiles:path ofType:fileTypes]]; } } NSArray *mediaFiles = [dirPath pathsMatchingExtensions:fileTypes]; for(NSString *fileName in mediaFiles){ fileName = [basePath stringByAppendingPathComponent:fileName]; [files addObject:fileName]; } return files; } 

你需要的是一个recursion的方法,以便你可以处理子目录。 以下第一种方法是公开的; 另一个私人。 想象一下,它们被实现为一个名为CocoaUtil的类的静态方法:

CocoaUtil.h:

 @interface CocoaUtil : NSObject + (NSArray *)findFilesWithExtension:(NSString *)extension inFolder:(NSString *)folder; @end 

CocoaUtil.m:

 // Private Methods @interface CocoaUtil () + (NSArray *)_findFilesWithExtension:(NSString *)extension inFolder:(NSString *)folder andSubFolder:(NSString *)subFolder; @end @implementation CocoaUtil + (NSArray *)findFilesWithExtension:(NSString *)extension inFolder:(NSString *)folder { return [CocoaUtil _findFilesWithExtension:extension inFolder:folder andSubFolder:nil]; } + (NSArray *)_findFilesWithExtension:(NSString *)extension inFolder:(NSString *)folder andSubFolder:(NSString *)subFolder { NSMutableArray *found = [NSMutableArray array]; NSString *fullPath = (subFolder != nil) ? [folder stringByAppendingPathComponent:subFolder] : folder; NSFileManager *fileman = [NSFileManager defaultManager]; NSError *error; NSArray *contents = [fileman contentsOfDirectoryAtPath:fullPath error:&error]; if (contents == nil) { NSLog(@"Failed to find files in folder '%@': %@", fullPath, [error localizedDescription]); return nil; } for (NSString *file in contents) { NSString *subSubFolder = subFolder != nil ? [subFolder stringByAppendingPathComponent:file] : file; fullPath = [folder stringByAppendingPathComponent:subSubFolder]; NSError *error = nil; NSDictionary *attributes = [fileman attributesOfItemAtPath:fullPath error:&error]; if (attributes == nil) { NSLog(@"Failed to get attributes of file '%@': %@", fullPath, [error localizedDescription]); continue; } NSString *type = [attributes objectForKey:NSFileType]; if (type == NSFileTypeDirectory) { NSArray *subContents = [CocoaUtil _findFilesWithExtension:extension inFolder:folder andSubFolder:subSubFolder]; if (subContents == nil) return nil; [found addObjectsFromArray:subContents]; } else if (type == NSFileTypeRegular) { // Note: case sensitive comparison! if ([[fullPath pathExtension] isEqualToString:extension]) { [found addObject:fullPath]; } } } return found; } @end 

这将返回一个包含指定文件扩展名的每个文件的完整path的数组。 请注意, [NSString pathExtension]不会返回. 的文件扩展名,所以一定不要通过extension参数。

是的,我们有NSArray下面的直接方法可以帮助你

  NSMutableArray *arrayFiles = [[NSMutableArray alloc] initWithObjects:@"a.png", @"a.jpg", @"a.pdf", @"h.png", @"f.png", nil]; NSLog(@"pathsMatchingExtensions----%@",[arrayFiles pathsMatchingExtensions:[NSArray arrayWithObjects:@"png", nil]]); //my output is "a.png", "h.png", "f.png" 

像这样,你可以find你的具体文件扩展名

  NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; NSFileManager *manager = [NSFileManager defaultManager]; NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot]; NSString *filename; while ((filename = [direnum nextObject] )) { if ([filename hasSuffix:@".doc"]) { //change the suffix to what you are looking for [arrayListofFileName addObject:[filename stringByDeletingPathExtension]]; } } 

使用下面的代码

 NSArray *myFiles = [myBundle pathsForResourcesOfType:@"Your File extension" inDirectory:nil];