File 如果存在Powershell文件,请使用sequencenumber重命名文件夹中的所有文件

File 如果存在Powershell文件,请使用sequencenumber重命名文件夹中的所有文件,file,powershell,sequence,rename,File,Powershell,Sequence,Rename,我有这个剧本: #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

我有这个剧本:

#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

当文件名已经存在时,必须给它一个序列号,如
照片\u LR\u 1000001\u 1.jpg,照片\u LR\u 1000001\u 2.jpg

但是,如果文件不存在,则不能添加序列号

我想要这个结构:
照片_LR_1000001.jpg
照片\u LR\u 1000001\u 1.jpg
照片\u LR\u 1000001\u 2.jpg
照片\u LR\u 1000001\u 3.jpg


我做错了什么?我想不出来。

这里有一些变化


$TrimmedName=$\u.name.substring(0,16)如果我第一次运行脚本,一切正常。 目录:C:\Temp\test\u脚本

模式LastWriteTime长度名称
-------------
-a-14/07/2011 21:36 46039照片\u LR\u 100001.jpg
-a--13/07/2011 2:29 41502照片\u LR\u 1000002.jpg
-a--4/03/2009 20:33 7569照片\u LR\u 1000003.jpg
-a--8/03/2009 2:33 14523照片\u LR\u 1000004.jpg
-a--7/03/2009 4:33 6754照片\u LR\u 100005.jpg
-a--6/03/2009 1:33 6536照片_LR_100005_1.jpg
-a--7/03/2009 20:33 11990照片\u LR\u 1000006.jpg
-a--8/03/2009 8:33 6939照片\u LR\u 100007.jpg
-a--6/03/2009 15:33 7656照片\u LR\u 1000007\u 1.jpg

但是如果我第二次运行它,它就不工作了。 目录:C:\Temp\test\u脚本

模式LastWriteTime长度名称
-------------
-a-14/07/2011 21:36 46039照片\u LR\u 100001.jpg
-a--13/07/2011 2:29 41502照片\u LR\u 1000002.jpg
-a--4/03/2009 20:33 7569照片\u LR\u 1000003.jpg
-a--8/03/2009 2:33 14523照片\u LR\u 1000004.jpg
-a--7/03/2009 4:33 6754照片\u LR\u 100005.jpg
-a--6/03/2009 1:33 6536照片_LR_100005_2.jpg
-a--7/03/2009 20:33 11990照片\u LR\u 1000006.jpg
-a--8/03/2009 8:33 6939照片\u LR\u 100007.jpg
-a--6/03/2009 15:33 7656照片\u LR\u 1000007\u 2.jpg

如您所见,
它不再保留PHOTO_LR_100007; u 1.jpg,而是将其重命名为PHOTO_LR_100007; u 2.jpg


脚本必须每周运行几次,因此重命名已重命名的文件是一个问题。

您好,谢谢您的回复!我添加了一个答案,因为它太长了,无法添加为注释。我在While循环中使用了一个检查来更新脚本,以检测更新的路径是否也是当前的全名(即,如果C:\Temp\Test\u script\PHOTO\u LR\u 100005\u 1.jpg=C:\Temp\Test\u script\PHOTO\u LR\u 100005\u 1.jpg,则中断循环)
#Variables
$src = "C:\Temp\test_script"
$dest = "C:\Temp\test_script"

Get-ChildItem -Path $src -Recurse | ForEach-Object {

    # Trim filename without sequencenumbers
    $TrimmedName= $_.name.substring(0,15) 
    # 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
    # Reset numerator in case trimmed file name is in use
    $num= 1

    # Make sure we don't fail the Test-Path because it finds and conflicts with itself
    if(!($NewPath -eq $_.FullName)){

    # If path exists loop untill there is a free sequence number 
    if (Test-Path $newpath) {
    while ((Test-Path $newpath))
    {
       $newpath = Join-Path $dest ($TrimmedName + "_$num" + $ext)    
       if(!($NewPath -eq $_.FullName)){
           $num+=1
       }
       else{break}
    }
    }
    # If path does not exist rename file without a sequence number
    $_.MoveTo($newpath)
}}