2014-09-20 76 views
0

我搜索了以及我發現的大部分答案,都不是我正在嘗試做的。我在目錄中有多個文件,我試圖通過在擴展名之前附加當前日期和時間作爲後綴來進行重命名。我已經在單個文件上多次執行過它,但無法爲大量文件工作。以下是我正在使用的代碼:通過附加當前日期在PowerShell中批量重命名文件

Get-ChildItem $Path -Filter "*.dat" -Recurse | Rename-Item -NewName {$_.Basename + '_' + $curDateTime + $_.Extension } 

它不會失敗,但文件不會重命名。

+2

你是如何分配$ curDateTime?例如,你不能在文件名中使用':'。這是否顯示您的日期/時間字符串? – 2014-09-20 04:32:25

回答

2

這個工作對我來說:

$curDateTime = Get-Date -Format yyyyMMdd-HHmmss 
Get-ChildItem $Path *.dat -Recurse | 
    Rename-Item -NewName {$_.Basename + '_' + $curDateTime + $_.Extension } -WhatIf 
+0

@keith_hill這對我很好,謝謝。它幫助我根據他們的時間戳組織我的照片。我開始學習PowerShell,並有一個[開放問題](https://superuser.com/questions/1117781/how-to-add-open-with-powershell-to-context-menu-of-folder-object-and-負載教授)還沒有回答,請檢查你是否想:) – sdkks 2016-08-27 17:40:16

0

另一種解決方案:

Get-ChildItem $Path -Filter "*.dat" -Recurse | 
    ren -New {$_.Name -replace '[.](?!.*?[.])',"_${curDateTime}."} -What 
+0

工作完美,謝謝 – user3235631 2014-09-20 14:13:34

相關問題