如何用 Windows 批处理脚本批量解压 ZIP 并重命名 MP3?
数据阿明
可以用 .bat 文件调用 PowerShell,完成解压、重命名和目录整理。这里采用题目允许的文件夹输出方式:每个 ZIP 对应一个同名文件夹,原压缩包保留。
前提是曲目编号已写在文件名末尾的括号中,例如 XYZ (1).mp3。脚本根据这个编号生成 01 这样的前缀,不读取 MP3 的 ID3 Track 标签,也不会根据文件排列顺序猜测编号。
处理后的目录是什么样的?
假设输入目录中有两个压缩包:
music/
Album A.zip
Album B.zip
rename-mp3.ps1
run.bat
其中 Album A.zip 包含:
XYZ (1).mp3
ZYZ (2).mp3
处理后得到:
music/
Album A.zip
Album B.zip
output/
Album A/
01 XYZ (1).mp3
02 ZYZ (2).mp3
Album B/
...
压缩包内原有的子目录和其他文件也会保留。
创建 PowerShell 脚本
下面的示例适用于 Windows PowerShell 5.1。将它保存为 rename-mp3.ps1,放在待处理 ZIP 所在的目录中。
param(
[string]$InputDir = $PSScriptRoot,
[string]$OutputDir = (Join-Path $PSScriptRoot 'output')
)
$ErrorActionPreference = 'Stop'
$InputDir = (Resolve-Path -LiteralPath $InputDir).ProviderPath
$OutputDir = [System.IO.Path]::GetFullPath($OutputDir)
New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null
# Only process ZIP files directly inside InputDir.
$archives = @(Get-ChildItem -LiteralPath $InputDir -Filter '*.zip' -File)
$failed = $false
foreach ($zip in $archives) {
$destination = Join-Path $OutputDir $zip.BaseName
$work = $null
if (Test-Path -LiteralPath $destination) {
Write-Warning "Skipped existing destination: $destination"
continue
}
try {
$work = Join-Path $OutputDir ([guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $work | Out-Null
Expand-Archive -LiteralPath $zip.FullName -DestinationPath $work
$mp3s = @(
Get-ChildItem -LiteralPath $work -Recurse -File -Force |
Where-Object { $_.Extension -ieq '.mp3' }
)
if ($mp3s.Count -eq 0) {
throw 'No MP3 files found in this archive.'
}
# Validate every filename before starting any renames.
$plan = foreach ($file in $mp3s) {
$m = [regex]::Match($file.BaseName, '\((?<track>[0-9]+)\)$')
if (-not $m.Success) {
throw "Missing trailing track number: $($file.Name)"
}
[int]$track = 0
if ((-not [int]::TryParse($m.Groups['track'].Value, [ref]$track)) -or $track -lt 1) {
throw "Invalid track number: $($file.Name)"
}
$newName = '{0:D2} {1}' -f $track, $file.Name
$target = Join-Path $file.DirectoryName $newName
if (Test-Path -LiteralPath $target) {
throw "Rename target already exists: $target"
}
[pscustomobject]@{
Path = $file.FullName
Name = $newName
}
}
foreach ($item in $plan) {
Rename-Item -LiteralPath $item.Path -NewName $item.Name
}
Move-Item -LiteralPath $work -Destination $destination
$work = $null
Write-Host "Done: $($zip.Name) -> $destination"
}
catch {
$failed = $true
Write-Warning ('Failed: {0}: {1}' -f $zip.Name, $_.Exception.Message)
}
finally {
if ($null -ne $work -and (Test-Path -LiteralPath $work)) {
Remove-Item -LiteralPath $work -Recurse -Force
}
}
}
if ($failed) {
exit 1
}
脚本先将文件解压到临时目录,检查全部 MP3 文件名,再执行重命名。如果某个压缩包处理失败,脚本会报告原因,清理该压缩包的临时目录,然后继续处理下一个。
创建批处理入口并运行
在同一目录创建 run.bat:
@echo off
powershell.exe -NoProfile -File "%~dp0rename-mp3.ps1"
exit /b %errorlevel%
在命令提示符中运行:
run.bat
%~dp0 表示批处理文件所在的目录,因此从其他工作目录启动时也能找到 PowerShell 脚本。路径两侧的双引号用于处理目录名中的空格。
也可以直接指定输入、输出目录:
powershell.exe -NoProfile -File "C:\Scripts\rename-mp3.ps1" -InputDir "D:\Music ZIPs" -OutputDir "D:\Renamed Music"
编号规则和注意事项
- 正则表达式
\((?<track>[0-9]+)\)$匹配不含扩展名的文件名末尾的编号,例如(1)、(02)或(123)。编号必须为正整数,且能转换为Int32。 D2表示至少两位十进制数字:1生成01,12生成12,123仍是123。- 原文件名完整保留,因此
XYZ (1).mp3会变成01 XYZ (1).mp3,符合题目中的示例。 - 如果任意 MP3 缺少末尾括号编号,整个压缩包会报告失败。若实际曲目编号来自独立的 track listing 或 ID3 标签,需要改写编号读取逻辑。
- 对应 ZIP 的输出文件夹已经存在时,脚本会跳过该 ZIP。需要重新处理时,先将已有输出文件夹移走,再运行脚本。
- 重命名目标已经存在时,脚本会报告失败,不覆盖现有文件。首次使用时可以先放入一个 ZIP,检查生成的目录和名称是否符合预期。
备注:内容仅供参考。
