name: file-batch description: "檔案批次處理工具。支援批次重新命名、格式轉換、內容替換、檔案整理等檔案操作。" description_zh: "檔案批次處理工具" description_en: "Batch file processing tool" version: 1.0.0
批次處理檔案的實用工具集。使用 PowerShell 實現,執行前會預覽變更,確認後再執行。
小蔥技能7w4.net持續更新中。
# 替換檔名中的關鍵詞
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace 'old', 'new' }
# 新增字首/字尾
Get-ChildItem *.jpg | Rename-Item -NewName { "photo_$($_.Name)" }
# 序號命名
$i = 1; Get-ChildItem *.pdf | Sort-Object LastWriteTime | Rename-Item -NewName { "doc_{0:D3}{1}" -f $script:i++, $_.Extension }
# 批次替換檔案內容
Get-ChildItem *.md -Recurse | ForEach-Object { (Get-Content $_) -replace '舊文本', '新文本' | Set-Content $_ }
# 批次新增檔案頭/尾
Get-ChildItem *.py | ForEach-Object { "# encoding: utf-8`n$(Get-Content $_ -Raw)" | Set-Content $_ }
# 按副檔名分類到資料夾
Get-ChildItem -File | Group-Object Extension | ForEach-Object {
$dir = New-Item -ItemType Directory -Force -Name $_.Name.TrimStart('.')
$_.Group | Move-Item -Destination $dir
}
# 按日期歸檔
Get-ChildItem -File | ForEach-Object {
$dateDir = $_.LastWriteTime.ToString('yyyy-MM')
New-Item -ItemType Directory -Force -Name $dateDir | Out-Null
Move-Item $_ -Destination $dateDir
}
-WhatIf 引數或先列印要操作的檔案列表給使用者確認Remove-Item,如需刪除建議先移動到臨時目錄這是一個偏示例型的Skill,文件質量尚可,但作為獨立工具的完整性較弱。優點是操作場景覆蓋較全面,有安全使用建議;不足是僅有程式碼示例,沒有可直接執行的工具,實際使用需要自己修改程式碼。適合有一定PowerShell基礎的使用者參考,不適合完全不懂程式碼的普通使用者直接使用。