【发布时间】:2014-02-21 03:54:54
【问题描述】:
我有这个脚本:
#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
当一个文件名已经存在时,它必须被赋予一个序列号,例如
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
...
我做错了什么?我想不通。
【问题讨论】:
标签: file powershell sequence rename