跳过正文

powershell重命名脚本

·164 字· loading · loading · · ·
lightmon
作者
lightmon
该博主太懒了,尚未添加简介。
目录

目的
#

把当前文件夹下的这些文件

image
重命名为
image

代码
#

Get-ChildItem -Path . -Filter "*.mkv" | ForEach-Object {
    if ($_.Name -ne "rename") {
        $fileName = $_.Name -replace "\.[^.]+$", ""
		# ?表示非贪婪模式
        $extension = $_.Name -replace '^.+\.', ''
        # (\d+\.?\d*) 匹配数字,包括小数点
        # (\(.*?\))? 匹配括号内的内容
        $pattern = '\[(\d+\.?\d*)(\(.*?\))?\]'
        if ($fileName -match $pattern) {
            # $s = [regex]::Match($fileName, '\[(.*?)\]').Value
            $match = $matches[1] # matches哈希表仅包含任何匹配模式的第一个匹配项
            # matches[1]即第一个()中的内容
            $newName = "S01E" + $match
            Rename-Item -LiteralPath $_.Name -NewName "$newName.$extension"
            Write-Output "$newName.$extension"
        }
        else {
            Write-Output "No match found"
        }
    }
}
Footer Background

© 2026 lightmon

Hugo & Blowfish 强力驱动