2025年1月27日 星期一

批次壓縮資料夾

## 前置需求 需安裝 7-zip ## 代碼 開新文字檔貼上下列代碼 ```bash @echo off rem 設定變數 ZIP_PATH,指定 7z 執行檔的路徑 set ZIP_PATH=c:\program files\7-zip\7z.exe rem 開始處理所有傳入的參數 for %%a in (%*) do ( rem 檢查 %%a 是否是資料夾 (使用 dir /a 查找並確認 "DIR" 字串) dir /a %%a|findstr "DIR" >nul 2>nul && ( rem 如果 %%a 是資料夾,則將該資料夾及其內容壓縮為 zip 檔案 "%ZIP_PATH%" a -tzip "%%~dpa%%~na.zip" "%%~a\*" ) || ( rem 如果 %%a 不是資料夾,則將該檔案壓縮為 zip 檔案 "%ZIP_PATH%" a -tzip "%%~dpa%%~na.zip" %%a ) ) ``` 編碼格式選擇:`ANSI` 檔名:`批次壓縮.bat` ## 如何使用 將個別或是多個資料夾拖曳到該批次檔即可執行壓縮 或是將該批次檔複製到SendTo,即可"右鍵->傳送到..."來執行批次壓縮的功能 ```bash win + R shell:sendto 或是 C:\Users\WindWater\AppData\Roaming\Microsoft\Windows\SendTo ``` ## 參考來源 http://nio127.blogspot.com/2012/01/httpwww.html chatGPT