[英]Using Powershell how can I bulk rename files recursively and include a counter in the new name?
我有一個項目,需要在包含特定字符串的多個子文件夾中搜索多個pdf,然后使用計數器變量將其重命名。 我是Powershell的新手,但是我認為它已經很接近了。 唯一不起作用的是計數器。 計數器變量永遠不會改變。 到目前為止,這是我需要的代碼:
$prefix = "sample"
$id = 1
Get-ChildItem -Filter "*combined*" -Recurse | Rename-Item -NewName ($prefix + ($id.tostring("000")) + '_regular_' + ($id.tostring("000")) + '.pdf' -f $id++ )
我想念什么? 我應該使用“ foreach”循環嗎?
是的,您需要使用ForEach
循環。 這將進行並進行重命名,以確保重命名后將子目錄中找到的所有文件保留在這些目錄中
Get-ChildItem -Filter "*combined*" -Recurse | %{
$fullId = $id.toString("000")
$curPath = Split-Path $_.FullName
Rename-Item $_.fullname -NewName "$curPath\$($prefix)$($fullId)_regular_$($fullId).pdf" )
$id += 1
}
這是上面代碼的編輯版本:
$id = 1
Get-ChildItem -Filter "*combined*" -Recurse | %{
$fullId = $id.toString("000")
$curPath = Split-Path $_.FullName
Rename-Item $_.fullname -NewName "$curPath\$($prefix)_$($fullId)_regular_$($fullId).pdf"
$id++
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.