I'm a PowerShell newbie so please be gentle! I'm looking to bulk rename a collection of files, then pad the first digits so that they play in order on my devices.
For example, I have a folder full of mp3s ABCDEF_S3L1_010221_GHIJK.mp3 through ABCDEF_S3L49_210921_GHIJK.mp3
I'm trying to rename them as 01-lesson.mp3 through 49-lesson.mp3 where the digit after the L is the prefix but I trip up with the padding for lessons 1-9 so they play out of order, i.e. 01 then 10, 11, 12 etc. I've tried using variations of .PadLeft and {0:D2} but keep getting errors such as:
Rename-Item: The filename, directory name, or volume label syntax is incorrect.
My code works without the initial padding:
PS G:\Temp\MP3s> Get-ChildItem -Path *.mp3 | Rename-Item
-NewName {$_.Name -replace '(^ABCDEF_S3L)(\d{1,2})_(\d{6})_(GHIJK)', '$2-lesson'}`
You can use
-matchand by doing so capture the number behind theLin$matches[1], which can be formatted like you want: