Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find characters and rename file name using Powershell

I have a large number of files in a directory with this type of naming convention: "1050_14447_Letter Extension.pdf", etc. I need to remove all characters before the 2nd underscore (including the 2nd underscore). So the new file name would be "Letter Extension.pdf". How can I iterate the single directory renaming accordingly?

like image 702
obautista Avatar asked Nov 29 '25 20:11

obautista


2 Answers

Here's an example to rename all pdf files in the current directory, removing all leading numbers or underscores from the file name:

Get-ChildItem -Filter *.pdf | Rename-Item -NewName {$_.Name -replace '^[0-9_]+'}
like image 52
Shay Levy Avatar answered Dec 02 '25 20:12

Shay Levy


Here's another method that doesn't rely on regex but assumes no underscores in the filepath:

Get-ChildItem 'C:\path2files' | %{
    Rename-Item $_.fullname $_.Name.Split("_")[2]
    }
like image 33
nimizen Avatar answered Dec 02 '25 18:12

nimizen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!