-2

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Coco
  • 21
  • 8
  • While your problem is well-defined, the down-votes likely stem from the fact that your question does not demonstrate any effort to solve it yourself (saying that you don't have a background in scripting isn't considered a mitigating circumstance). – mklement0 Aug 02 '22 at 01:40

1 Answers1

1
# 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:

mklement0
  • 382,024
  • 64
  • 607
  • 775