Home > Mobile >  How to add month and year in pdf batch file using powerscript?
How to add month and year in pdf batch file using powerscript?

Time:08-02

I have 500 files that needed to be renamed from: surname_dtr.pdf to surname_YYYYMMdtr.pdf where YYYY is the current year, and MM is the previous month. I have known that this is possible using script. I do not have a background in writing a script that's why I am looking for an answer here, but no avail.

CodePudding user response:

# 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

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

See also:

  • Related