2014-01-29 26 views
1

我有這樣的腳本:重命名文件夾中的所有文件的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
...

我在做什麼錯?我無法弄清楚。

回答

1

這裏只是一些變化......

$ TrimmedName = $ _ name.substring(0,16)< - 需要爲(0.15),因爲我們從0開始,而不是1。

我將您的Rename-Item更改爲使用內置的$ _。MoveTo()方法。你的目的似乎更簡單。因爲你基本上是在告訴它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) 
}} 

如果您有任何疑問或問題,請發表評論,但它對我的測試很有效。

+0

您好,非常感謝您的回覆!我添加了一個答案,因爲將它添加爲評論太長了。 – Yadeki

+1

我在While循環中更新了腳本,如果更新的路徑也是當前的全名(即'如果C:\ Temp \ Test_Script \ PHOTO_LR_100005_1.jpg = C:\ Temp \ Test_Script \ PHOTO_LR_100005_1.jpg然後斷開循環) – TheMadTechnician

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 --- 2009年8月3日2:33 14523 PHOTO_LR_1000004.jpg
-a --- 2009年7月3日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 --- 2009年8月3日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 --- 4/03/2009 20:33 7569 PHOTO_LR_1000003.jpg
-a --- 2009年8月3日2:33 14523 PHOTO_LR_1000004.jpg
-a --- 2009年7月3日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_2.jpg的PHOTO_LR_1000007_1.jpg。

該腳本必須每週運行幾次,因此重命名已重命名的文件是一個問題。

相關問題