PowerShell批量改名

发布时间 2023-10-28 23:10:33作者: 徐影魔
$files = Get-Location | Get-ChildItem

$current = Get-Location

Write-Output $current.Path

for ($i = 0; $i -lt $files.Length; $i++) {

    $file = $files[$i]

    if ($file.FullName.EndsWith(".png")) {

        Write-Output "-------------------"  

        $array = $file.Name.Replace("等级勋章/", "").Split("@")

        $b = $array[1].Replace("2x(", "").Replace(").png", "")

        $num = 0

        if (-Not($b.Equals("2x.png"))) {

            $num = $b -as [int]

        }

        $level = ($array[0].Split("-")[0] -as [int]) + $num

        $resultName =  $file.Directory.FullName+"\"+("level_"+$level+".png")

        Rename-Item -Path $file.FullName -NewName $resultName

        Write-Output ("Rename "+$file.FullName+" to " +$resultName)

        # Write-Output $array[0]

        # Write-Output $num

    }

}

#code