Windows批量重命名脚本

0

# UTF-8 with BOM

$OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

#$fileList = Get-ChildItem -Path .\* -File -Include *.jpg,*.png,*.gif,*.bmp,*.webp,*.jpeg | Sort-Object {
#    [regex]::Replace($_.Name, "\d+", { $args[0].Value.PadLeft(20, "0") })
#}

$fileList = Get-ChildItem -Path . -File | Sort-Object {
    [regex]::Replace($_.Name, "\d+", { $args[0].Value.PadLeft(20, "0") })
}
$i=1
$fileList | ForEach-Object {
    $newName = "{0:0000}{1}" -f $i++,$_.Extension
	Write-Host "$_ -> $newName"
}
Write-Host "输入【C】继续执行(其他按键直接退出)"
$key = [Console]::ReadKey($true)
if ($key.Key -notin 'C','c') {
    Write-Host "取消操作"
    exit
}
$i=1
$fileList | ForEach-Object {
    $newName = "{0:0000}{1}" -f $i++,$_.Extension
    if ($_.Name -ne $newName) {
        Write-Host "$_ -> $newName"
        Rename-Item $_.FullName $newName
    } else {
        Write-Host "跳过$_"
    }
}
Write-Host "完成操作"