PowerShell PadRight Not Adding Specified # of Characters

Viewed 153

I have a series of files that I want to rename. I want to rename the files with padded zeroes (total of 3 characters) after the first occurrence of the "-" delimiter like the examples below:

Old Name Example 1: 101-1_File1_Project1-000-END.txt
Desired New Name Example 1: 101-001_File1_Project1-000-END.txt

Old Name Example 2: 101-28_File1_Project1-000-END.txt
Desired New Name Example 2: 101-028_File1_Project1-000-END.txt

PowerShell code below works for Example 1 and not for Example 2. For Example 2 the result is:
101-0028_File1_Project1-000-END.txt

I thought this situation is exactly what PadRight is supposed to handle where there will be no more than 3 characters to the right of the "-". Can someone please assist?

PowerShell:

gci "C:\Path\Folder-with-Files" | ren -n {[regex]::replace($_.basename, '(?<=^[^-]*)-', {"$args".PadRight(3, '0')})+ $_.extension}
2 Answers

There are two problems with your attempt:

  • Your regex doesn't actually capture the number to pad.

  • You're using .PadRight() instead of .PadLeft()

Therefore (note the \d+ in the regex):

'101-1_File1_Project1-000-END.txt', '101-28_File1_Project1-000-END.txt' | % {
  [regex]::Replace($_, '(?<=^[^-]+-)\d+', { $args[0].Value.PadLeft(3, '0') })
}

Note that in PowerShell (Core) you can now use the -replace operator with a script block:

'101-1_File1_Project1-000-END.txt', '101-28_File1_Project1-000-END.txt' | % {
  $_ -replace '(?<=^[^-]+-)\d+', { $_.Value.PadLeft(3, '0') }
}

Both commands output the following:

101-001_File1_Project1-000-END.txt
101-028_File1_Project1-000-END.txt

How about using a loop and the format operator "-f" to achieve what you need? This should work actually:

Get-ChildItem -Path 'C:\Path\Folder-with-Files' |
ForEach-Object {
    If ($_.Name -match '(?<=^.*-)(\d+)(?=_)') {
        $Replace = '{0:00#}' -f ($Matches[1] -as [Int16])
        $NewName = $_.Name -replace '(?<=^.*-)(\d+)(?=_)', $Replace
        Rename-Item -Path $_.FullNAme -NewName $NewName
    }
}
Related