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

Time:09-27

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

CodePudding user response:

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) }

CodePudding user response:

Cheating and going to powershell 7 with the scriptblock replace. You just have to workout where the 2nd group match is within the $_ match object, which turns out to be $_.groups[2].value. Then you can use .padleft(2,'0') on it.

# echo hi | set-content ABCDEF_S3L1_010221_GHIJK.mp3,
#   ABCDEF_S3L49_210921_GHIJK.mp3


Get-ChildItem -Path *.mp3 | 
  % { $_.Name -replace '(^ABCDEF_S3L)(\d{1,2})_(\d{6})_(GHIJK)', 
  { $a = $_ } }

.mp3
.mp3


$a

Groups   : {0, 1, 2, 3, 4}
Success  : True
Name     : 0
Captures : {0}
Index    : 0
Length   : 25
Value    : ABCDEF_S3L49_210921_GHIJK


Get-ChildItem -Path *.mp3 | 
  rename-item -newname { 
  $_.Name -replace '(^ABCDEF_S3L)(\d{1,2})_(\d{6})_(GHIJK)', 
  { $_.groups[2].value.padleft(2,'0')   '-lesson' } } -whatif

What if: Performing the operation "Rename File" on target 
      "Item: C:\Users\js\foo\ABCDEF_S3L1_010221_GHIJK.mp3 
Destination: C:\Users\js\foo\01-lesson.mp3".
What if: Performing the operation "Rename File" on target 
      "Item: C:\Users\js\foo\ABCDEF_S3L49_210921_GHIJK.mp3 
Destination: C:\Users\js\foo\49-lesson.mp3".
  • Related