i need a regular expression for renaming hundreds of pdf files with the PowerRename tool of the Microsoft PowerToys toolbox or alternatively with Windows PowerShell. The files are named like this:
100_20_Mustermann_Max_something_else.pdf
5421_826_Mustermann_Sam_something_else.pdf
I want to replace the third underline "_" with a comma plus space ", " The result should be:
100_20_Mustermann, Max_something_else.pdf
5421_826_Mustermann, Sam_something_else.pdf
I am not familiar with regEx can someone help?
CodePudding user response:
gci | % {
$newName = $_.FullName -replace '([^_] )_([^_] )_([^_] )_([^_] )_([^_] )_(. )', '$1_$2_$3, $4_$5_$6'
Rename-Item -Path $_.FullName $newName -WhatIf
}
This Powershell should work for you given that all the files in the directory the script is run, match the file name examples provided.
It's just a straightforward capture of the 6 groups around the 5 underscores and then just rebuilt with the third underscore substitution.
You can remove the -WhatIf safety catch once you are happy to go ahead and rename the files for real.