PhotoBatch-文件重命名(2)

上一篇文章已經(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 = @"處理完成";
    
}

demo地址:https://github.com/macOSApp/photoBatch

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,983評論 25 709
  • Ubuntu的發(fā)音 Ubuntu,源于非洲祖魯人和科薩人的語言,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲de夢閱讀 100,621評論 9 468
  • 她 初長 深閨院 小徑幽長 春來綠蘿裳 云髻輕斜挽簪 嬌笑幾分眉情婉 花見猶憐不覺輕嘆 柳絮因風四下飄散亂 只愿花...
    伶人雨閱讀 556評論 0 1
  • 最近聽到一個概念,叫“麻煩守恒定律”,就是說,麻煩的總量是不變的,解除麻煩可以靠工具和人,工具承擔解決麻煩的任務多...
    成長是剛需閱讀 699評論 0 0

友情鏈接更多精彩內容