Re: [請問] 以資料夾為名稱針對資料夾內圖片的批量壓縮方法

看板EZsoft (小軟體)作者 (falken)時間3月前 (2024/06/03 15:01), 3月前編輯推噓2(200)
留言2則, 2人參與, 2月前最新討論串2/2 (看更多)
先安裝 NanaZip 或 7-Zip 在 PowerShell 中輸入 7z.exe 確認是否可用 若得到以下訊息則必須手動新增 7z.exe 所在目錄到環境變數 PATH "無法辨識 '7z.exe' 詞彙是否為 Cmdlet、函數、指令檔或可執行程式的名稱。......" 在 PowerShell 中執行下列命令 使用前記得先修改 Path\To\Dir 為你要的主目錄路徑 Set-Location -LiteralPath 'Path\To\Dir' $outputDirPath = $PWD.Path foreach ($item in (Get-ChildItem -LiteralPath .)) { $desFilePath = Join-Path $outputDirPath "$($item.Name).zip" if ($item.PsIsContainer) { $souFilePath = Join-Path $item.FullName * } else { $souFilePath = $item.FullName } & 7z.exe a "$($desFilePath)" "$($souFilePath)" } 你也可只用 PowerShell 的 cmdlet 來達到相同功能 Set-Location -LiteralPath 'Path\To\Dir' $outputDirPath = $PWD.Path foreach ($item in (Get-ChildItem -LiteralPath .)) { $desFilePath = Join-Path $outputDirPath "$($item.Name).zip" $escDesFilePath = $desFilePath -replace '[\`\[\]]', '`$0' if ($item.PsIsContainer) { $escSouFileParentPath = $item.FullName -replace '[\`\[\]]', '`$0' $escSouFilePath = Join-Path $escSouFileParentPath * } else { $escSouFilePath = $item.FullName -replace '[\`\[\]]', '`$0' } Compress-Archive -Path $escSouFilePath -DestinationPath $escDesFilePath } 以下是補充說明 - 路徑中單獨的 . 等同 $PWD.Path - Get-ChildItem 'path\to\dir' 等同 Get-Item 'path\to\dir\*' - 用參數 -LiteralPath 指定輸入,則不會將路徑中的任何字元視為特殊字元 - 若只是要在路徑中使用 * 字元,則可用 -replace 跳脫路徑中的方括號字元 - 工作目錄中帶有特殊字元可能會導致某些 cmdlet 找不到相對路徑的目標,所以餵給 cmdle t 的路徑最好是絕對路徑。 例如把 .\file.ext 轉成絕對路徑的方法如下 $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.\file.ext') 大概就這樣… -- Sent from PTTopia -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 27.247.165.180 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/EZsoft/M.1717398069.A.841.html ※ 編輯: falcon (27.247.165.180 臺灣), 06/03/2024 15:17:47 ※ 編輯: falcon (27.247.165.180 臺灣), 06/03/2024 15:26:03

06/05 20:22, 3月前 , 1F
感激~ f 大提供的程式~晚點來測試一下 m(_ _)m
06/05 20:22, 1F

06/22 21:25, 2月前 , 2F
06/22 21:25, 2F
文章代碼(AID): #1cNMerX1 (EZsoft)
文章代碼(AID): #1cNMerX1 (EZsoft)