简体   繁体   中英

Regular Expression for renaming Files

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM