powershellrename-item-cmdlet

How to batch rename files in PowerShell starting with padded first numbers


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'}`

Solution

  • You can use -match and by doing so capture the number behind the L in $matches[1], which can be formatted like you want:

    Get-ChildItem -Path 'D:\Test' -Filter '*.mp3' -File | 
        # apply better filename filter and capture the number after the 'L' in $matches[1]
        Where-Object { $_.BaseName -match '^\w+_S\dL(\d+)_\d+_\w+' } | 
        Rename-Item -NewName { ('{0:D2}-lesson{1}' -f [int]$matches[1], $_.Extension) }