тестируйте
Код:
get-childitem $PSScriptRoot -Directory | % {
$dir = $_; $count = 0;
$dir | get-childitem -File | % {
if ($_.Name -match '^(k\d+_)(\d+)(\.png)$') {
[pscustomobject]@{ N = [int]$matches[2]; M = $matches; O = $_; }
}
} | sort-object 'N' | % { ++$count;
while (test-path ($newpath = join-path $dir.fullname ($name = $_.M[1] + $count + $_.M[3]))) {
if ($_.O.fullname -eq $newpath) { return }
++$count;
}
'rename: {0} -> {1}' -f $_.O.fullname, $name
$_.O.MoveTo($newpath);
}
}