通过附加当前日期在PowerShell中批量重命名文件

问题描述:

我搜索了以及我发现的大部分答案,都不是我正在尝试做的。我在目录中有多个文件,我试图通过在扩展名之前附加当前日期和时间作为后缀来进行重命名。我已经在单个文件上多次执行过它,但无法为大量文件工作。以下是我正在使用的代码:通过附加当前日期在PowerShell中批量重命名文件

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

它不会失败,但文件不会重命名。

+2

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

这个工作对我来说:

$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

另一种解决方案:

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

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