我有500个文件需要从surname_dtr.pdf
重命名为surname_YYYYMMdtr.pdf
,其中YYYY
是当年,MM
是上月。我知道这是可能的使用脚本。我没有写剧本的背景,这就是为什么我在这里寻找答案,但无济于事。
# Get the previous month's string representation in the desired format.
$timestampStr = (Get-Date -Day 1).AddDays(-1).ToString('yyyyMM')
# Rename all files of interest in the current dir.,
# using a delay-bind script block.
Get-ChildItem -Filter *_dtr.pdf |
Rename-Item -NewName { $_.BaseName + $timestampStr + $_.Extension } -WhatIf
注意:上面命令中的-WhatIf
公共参数预览操作。一旦您确定操作将执行您想要的操作,请删除-WhatIf
另请参阅:
Get-Date
Rename-Item
- 延迟绑定脚本块