我有這樣的腳本:重命名文件夾中的所有文件的sequenceNumber如果文件存在Powershell的
#Variables
$src = "C:\Temp\test_script"
$dest = "C:\Temp\test_script"
$num= 1
Get-ChildItem -Path $src -Recurse | ForEach-Object {
# Trim filename without sequencenumbers
$TrimmedName= $_.name.substring(0,16)
# Get extension of filename
$ext= $_.Extension
# Get filename without sequencenumber but with extension
$FullName= $TrimmedName + $ext
# Get the path to be used
$newpath = Join-Path -Path $dest -ChildPath $FullName
# If path exists loop untill there is a free sequence number
if (Test-Path -Path $newpath) {
while ((Test-Path -Path $newpath) -eq $true)
{
$newpath = Join-Path $dest ($TrimmedName + "_$num" + $_.Extension)
$num+=1
}
Rename-Item $_.pspath -NewName $newpath
}
# If path does not exist rename file without a sequence number
else { Rename-Item $_.pspath -NewName $newpath }
}
我想要做的是一個文件夾,這種類型的文件重命名:PHOTO_LR_1000001.jpg
在此文件夾也有文件是這樣的:
PHOTO_LR_1000001_2.jpg或PHOTO_LR_1000001 2.JPG或PHOTO_LR_1000001_V2.jpg或PHOTO_LR_1000001 V2.jpg
當一個文件名已經存在,必須給像
序列號210 PHOTO_LR_1000001_1.jpg,PHOTO_LR_1000001_2.jpg,...
但是,當文件尚未存在時,它不能添加序列號。
我想有這樣的結構:
PHOTO_LR_1000001.jpg
PHOTO_LR_1000001_1.jpg
PHOTO_LR_1000001_2.jpg
PHOTO_LR_1000001_3.jpg
...
我在做什麼錯?我無法弄清楚。
您好,非常感謝您的回覆!我添加了一個答案,因爲將它添加爲評論太長了。 – Yadeki
我在While循環中更新了腳本,如果更新的路徑也是當前的全名(即'如果C:\ Temp \ Test_Script \ PHOTO_LR_100005_1.jpg = C:\ Temp \ Test_Script \ PHOTO_LR_100005_1.jpg然後斷開循環) – TheMadTechnician