【问题标题】:Rename all files in a folder with sequencenumber if file exists Powershell如果文件存在Powershell,则使用序列号重命名文件夹中的所有文件
【发布时间】: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


    【解决方案1】:

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

    $TrimmedName= $_.name.substring(0,16)

    我更改了您的 Rename-Item 以使用内置的 $_.MoveTo() 方法。就您的目的而言,这似乎更简单。

    另外,我在 ForEach 循环结束时对其进行了一次调用,因为您基本上是在告诉它 If(try $newname) Then (update $newname until unique; use $newname) else (use $newname)所以我刚刚删除了由于您以一种或另一种方式重命名文件的冗余。

    添加了检查以确保您的测试路径不会因为找到自己而失败。

    将 $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)
    }}
    

    如果您有任何疑问或问题,请发表评论,但它对我的测试很有效。

    【讨论】:

    • 您好,感谢您的回复!我添加了一个答案,因为它太长了,无法添加为评论。
    • 我已经更新了脚本,在 While 循环中进行了检查,以判断更新后的路径是否也是当前全名(即if C:\Temp\Test_Script\PHOTO_LR_100005_1.jpg = C:\Temp\Test_Script\PHOTO_LR_100005_1.jpg then break loop
    【解决方案2】:

    如果我第一次运行脚本一切正常。 目录: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--- 2009 年 4 月 3 日 20:33 7569 PHOTO_LR_1000003.jpg
    -a--- 2009 年 8 月 3 日 2:33 14523 PHOTO_LR_1000004.jpg
    -a--- 7/03/2009 4:33 6754 PHOTO_LR_1000005.jpg
    -a--- 2009 年 6 月 3 日 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--- 2009 年 6 月 3 日 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--- 2009 年 4 月 3 日 20:33 7569 PHOTO_LR_1000003.jpg
    -a--- 2009 年 8 月 3 日 2:33 14523 PHOTO_LR_1000004.jpg
    -a--- 7/03/2009 4:33 6754 PHOTO_LR_1000005.jpg
    -a--- 2009 年 6 月 3 日 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--- 2009 年 6 月 3 日 15:33 7656 PHOTO_LR_1000007_2.jpg

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      相关资源
      最近更新 更多