powershell重命名脚本

目的

把当前文件夹下的这些文件
image
重命名为
image

代码

Get-ChildItem -Path . -Filter "*.mkv" | ForEach-Object {
    if ($_.Name[0] -ne "1") {
        $fileName = $_.Name -replace "\.[^.]+$", ""
        $pattern = '\[\d+\.?\d*\]'
        if ($fileName -match $pattern) {
            $match = $matches[0]
            $newName = "0x" + $match.Trim('[', ']')
            Write-Output $newName
            Rename-Item -LiteralPath $_.FullName -NewName "$newName.mkv"
        }
        else {
            Write-Output "No match found"
        }
    }
}
posted @ 2024-07-18 22:46  lightmon  阅读(1)  评论(0编辑  收藏  举报