Share via


Powershell, renaming files with regular expression or other methods

Question

Sunday, August 25, 2013 12:08 PM

Hi there,

I need to run a script across about 2.5K files, to rename them slightly.

e.g. 93250917[S077790-3D].jpg  should be 93250917.jpg 

So i need to remove the brakcets and everything in between. The first few numbers are variable length an not set amount of digits as is everything in between the brackets. I am totally useless when it comes to regex so hoping someone can help me out.

Much appreciated.

All replies (4)

Sunday, August 25, 2013 1:14 PM

Ok I found the regex i need using http://gskinner.com/RegExr/ wonderful site.

So with powershell i need to replace the regex  \.*\ with nothing. 

Any help would be appreciated.


Sunday, August 25, 2013 2:02 PM | 1 vote

You could try the following command within the file path of those files you want to rename:

Get-ChildItem . -file -Filter "*.jpg" | Rename-Item -newname { $_.name -replace "\.+\\s*", ""}

wizend


Sunday, August 25, 2013 2:02 PM | 2 votes

You're going to run into some errors if you have any potential duplicate names, but the basic idea is:

Get-ChildItem | Rename-Item -NewName { $_.Name -replace '\[.*\]' }

Sunday, August 25, 2013 3:33 PM

Thank you both, I managed to get it working with your suggestions, i did run into a few renaming duplicates, but this command helped identify them so i thank you both for this help.