获取文件夹下某个后缀的所有文件
获取ios_patch文件夹下面pat后缀的所有文件
NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *baseSavePath = [documentPath stringByAppendingString:@"/IOSBundle/ios_patch/"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *myDirectoryEnumerator = [fileManager enumeratorAtPath:baseSavePath]; //baseSavePath 为文件夹的路径
NSMutableArray *filePathArray = [[NSMutableArray alloc]init]; //用来存目录名字的数组
NSString *file;
while((file=[myDirectoryEnumerator nextObject])) //遍历当前目录
{
if([[file pathExtension] isEqualToString:@"pat"]) //取得后缀名为.pat的文件名
{
[filePathArray addObject:[baseSavePath stringByAppendingPathComponent:file]];//存到数组
}
}
NSString *patch1Path = [filePathArray lastObject];
删除文件夹下面的所有内容
删除IOSBundle文件夹下的所有文件
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *bundlePath = [documentsPath stringByAppendingPathComponent:@"IOSBundle"];
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:bundlePath];
NSString *fileName;
while (fileName= [dirEnum nextObject]) {
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@",path,fileName] error:nil];
}
删除文件路径(文件夹)
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *bundlePath = [documentsPath stringByAppendingPathComponent:@"IOSBundle"];
[fileManage removeItemAtPath:bundlePath error:nil];
根据文件名在某个路径下获取文件
NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *baseBundleSavePath = [appDir stringByAppendingString:@"/BaseBundle/output/ios/bundle"];
NSString *docPath = [NSBundle pathForResource:@"main" ofType:@"jsbundle" inDirectory:baseBundleSavePath];
创建文件路径(文件夹)
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *bundlePath = [documentsPath stringByAppendingPathComponent:@"IOSBundle"];
//是否存在文件夹IOSBundle,不存在则创建
if (![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:bundlePath withIntermediateDirectories:YES attributes:nil error:nil];
}
删除文件夹下面后缀结尾的文件
NSString *extension = @"png";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSEnumerator *enumerator = [contents objectEnumerator];
NSString *filename;
while ((filename = [enumerator nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:nil];
}