上一篇文章已經(jīng)寫了如何獲取文件夾路徑撩鹿,今天實現(xiàn)photoBatch的第一個簡單功能谦炬,圖片批量重命名,當然也可以對任何文件進行重命名
搭建界面
搭了個簡單的界面,如下圖:
布局用了purelayout框架
我們需要得到重命名后的文件名前綴键思,以及文件格式和是否保留原文件,給這3個變量一個默認值吼鳞。
文件夾處理
獲取到文件路徑后處理:需要對每個文件的路徑進行處理看蚜,如果是以
file://
開頭,需要把前面的file://
去掉赔桌,這種地址無法處理供炎。這里是對文件夾路徑的處理,處理完的路徑再加上文件名就是完整的路徑疾党。
- (void)dealFiles:(NSArray *)filepaths
{
self.dealingLabel.stringValue = [filepaths.firstObject description];
NSMutableArray *arr = [NSMutableArray new];
// 對文件夾路徑進行處理
for (NSString *path in filepaths) {
if ([[path description] hasPrefix:@"file:///"]) {
NSString *newpath = [[path description] substringFromIndex:7];
if ([newpath hasSuffix:@"/"]) {
newpath = [newpath substringToIndex:newpath.length - 1];
}
[arr addObject:newpath];
} else {
if ([[path description] hasSuffix:@"/"]) {
NSString *tempStr = [path description];
[arr addObject:[tempStr substringToIndex:tempStr.length - 1]];
} else {
[arr addObject:[path description]];
}
}
}
self.folderPaths = filepaths;
NSMutableArray *allFiles = [NSMutableArray new];
for (NSString *docuPath in self.folderPaths) { // 遍歷所有文件夾 獲取所有文件個數(shù)
NSArray *files = [XCFileManager listFilesInDirectoryAtPath:docuPath deep:NO];//這里遍歷得到的只是文件名
[allFiles addObjectsFromArray:files];
}
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"文件獲取成功"];
[alert setInformativeText:[NSString stringWithFormat:@"文件總數(shù):%ld 個", allFiles.count]];
[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) {
}];
}
文件批量重命名
遍歷所有文件夾下所有文件音诫,NSFileManager
并沒有重命名的方法,如果要保留原文件雪位,則執(zhí)行copy
操作竭钝,如果不保留原文件,則執(zhí)行move
操作雹洗。下面是重命名代碼的實現(xiàn)香罐。
- (IBAction)StartAction:(NSButton *)sender {
NSMutableArray *allFiles = [NSMutableArray new];
for (NSString *docuPath in self.folderPaths) {
NSArray *files = [XCFileManager listFilesInDirectoryAtPath:docuPath deep:NO];//這里遍歷得到的只是文件名
for (NSString *filename in files) {
[allFiles addObject:[NSString stringWithFormat:@"%@/%@", docuPath, filename]];
}
}
if (allFiles.count == 0) {
return;
}
NSString *resultFilePath = [NSString stringWithFormat:@"%@/%@", self.folderPaths.firstObject, @"result"];
NSError *err = nil;
[XCFileManager createDirectoryAtPath:resultFilePath error:&err];
NSString *prefixName = _reNameView.prefixInput.stringValue;
if (!prefixName || prefixName.length == 0) {
prefixName = @"img_";
}
NSString *suffixName = _reNameView.suffixInput.stringValue;
if(!suffixName || suffixName.length == 0) {
suffixName = @"";
}
NSInteger index = 1;
NSString *suffix = @"";
for (NSString *path in allFiles) {
// // 如果遇到 沒有文件名的文件,直接過濾
if ([path componentsSeparatedByString:@"."].count < 2) {
continue;
}
if (suffixName.length == 0) {
suffix = [[path componentsSeparatedByString:@"."].lastObject description];
} else {
suffix = suffixName;
}
self.dealingLabel.stringValue = [path description];
NSString *movePath = [NSString stringWithFormat:@"%@/%@%ld.%@", resultFilePath, prefixName, index,suffix];
if (_reNameView.checkSaveBtn.state == 1) {
[XCFileManager moveItemAtPath:path toPath:movePath overwrite:NO];
} else {
[XCFileManager moveItemAtPath:path toPath:movePath overwrite:YES];
}
index ++;
}
self.dealingLabel.stringValue = @"處理完成";
}