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

时间:2014-01-29 11:19:01

标签: 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 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
...

我做错了什么?我无法理解。

2 个答案:

答案 0 :(得分:1)

这里只是一些变化......

$ TrimmedName = $ _。name.substring(0,16)< - 需要为(0,15),因为我们从0开始,而不是1。

我将Rename-Item更改为使用内置的$ _.MoveTo()方法。对你的目的来说似乎更简单。

另外,我在ForEach循环结束时对它进行了一次调用,因为你基本上是在告诉它If(尝试$ newname)然后(更新$ newname直到唯一;使用$ newname)else(使用$ newname)所以我只是删除了冗余,因为你以某种方式重命名文件。

添加了检查以确保您的Test-Path没有失败,因为它找到了自己。

将$ num = 1移到ForEach循环中,以便它自动重置每个文件。

#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)
}}

如果您有任何问题或问题,请发表评论,但它对我的测试工作正常。

答案 1 :(得分:0)

如果我第一次运行脚本一切正常。     目录:C:\ Temp \ test_script

模式LastWriteTime长度名称
---- ------------- ------ ----
-a --- 14/07/2011 21:36 46039 PHOTO_LR_1000001.jpg
-a --- 13/07/2011 2:29 41502 PHOTO_LR_1000002.jpg
-a --- 4/03/2009 20:33 7569 PHOTO_LR_1000003.jpg
-a --- 8/03/2009 2:33 14523 PHOTO_LR_1000004.jpg
-a --- 7/03/2009 4:33 6754 PHOTO_LR_1000005.jpg
-a --- 6/03/2009 1:33 6536 PHOTO_LR_1000005_1.jpg
-a --- 7/03/2009 20:33 11990 PHOTO_LR_1000006.jpg
-a --- 8/03/2009 8:33 6939 PHOTO_LR_1000007.jpg
-a --- 6/03/2009 15:33 7656 PHOTO_LR_1000007_1.jpg

但如果我第二次运行它,它就不再起作用了。 目录:C:\ Temp \ test_script

模式LastWriteTime长度名称
---- ------------- ------ ----
-a --- 14/07/2011 21:36 46039 PHOTO_LR_1000001.jpg
-a --- 13/07/2011 2:29 41502 PHOTO_LR_1000002.jpg
-a --- 4/03/2009 20:33 7569 PHOTO_LR_1000003.jpg
-a --- 8/03/2009 2:33 14523 PHOTO_LR_1000004.jpg
-a --- 7/03/2009 4:33 6754 PHOTO_LR_1000005.jpg
-a --- 6/03/2009 1:33 6536 PHOTO_LR_1000005_2.jpg
-a --- 7/03/2009 20:33 11990 PHOTO_LR_1000006.jpg
-a --- 8/03/2009 8:33 6939 PHOTO_LR_1000007.jpg
-a --- 6/03/2009 15:33 7656 PHOTO_LR_1000007_2.jpg

如你所见,
而不是保留PHOTO_LR_1000007_1.jpg,而是将其重命名为PHOTO_LR_1000007_2.jpg。

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

相关问题