// 编写一个程序,将一个目录下的文件分类管理。
// 用每个文件的尾缀,创建对应全大写目录,将相关文件移动到对应目录中,如有 1.txt 文件,创建 TXT 目录,将 1.txt 移动到这个目录下,另有一个目录( subDir )存储子目录, 其他无尾缀文件,放入 Others 目录。
// 【不支持双尾缀】1.txt.zip
-(void)SelectFiles:(NSString*)path { *****获取IOS 各个路径 ///document NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);//程序主目录 NSString *documentDic = [paths objectAtIndex:0]; NSFileManager *fm = [NSFileManager defaultManager]; NSError *error = nil; //*******创建subDir NSString *subDirPath = [path stringByAppendingString:@"subDir"]; //判断subDir文件夹是否存在 if(![fm fileExistsAtPath:subDirPath]) { [fm createDirectoryAtPath:subDirPath withIntermediateDirectories:YES attributes:nil error:&error]; if(error) { NSLog(@"create subPath Error:%@",error); exit(-1); } } //*******创建other NSString *otherPath = [path stringByAppendingString:@"other"]; if(![fm fileExistsAtPath:otherPath]) { [fm createDirectoryAtPath:otherPath withIntermediateDirectories:YES attributes:nil error:&error]; if(error) { NSLog(@"create otherPath Error:%@",error); exit(-1); } } //*******遍历path目录下的文件 NSArray *array = [fm contentsOfDirectoryAtPath:path error:&error]; if(error) { NSLog(@"get path file Array Error:%@",error); exit(-1); } for(NSString *strPath in array) { NSArray *arr = [strPath componentsSeparatedByString:strPath]; if(arr.count>1)//带后缀的文件 { //根据后缀名来创建目录名 NSString *suffixPath = [arr[arr.count-1] uppercaseString]; NSString *dirName = [suffixPath stringByAppendingString:subDirPath]; if(![fm fileExistsAtPath:dirName]) { [fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:&error]; } //移动文件 [fm moveItemAtPath:strPath toPath:[dirName stringByAppendingString:path] error:&error]; } else//不带后缀名 { if([strPath isEqualToString:@"subDir"] || [strPath isEqualToString:@"Others"]) { continue; } else { //移动文件 [fm moveItemAtPath:[path stringByAppendingPathComponent:strPath] toPath:[otherPath stringByAppendingPathComponent:strPath] error:&error]; } } } }