Wednesday, October 26, 2011

Powershell rename file using regex

I had a task where I need to update file names of thousands of files. My problem was to rename files of form to '[0-9]{5} .*' to '[0-9]{5}.*' (no white space between base number and file name). Thanks to great powershell, I could finish it in couple of minutes. I used grouping and back references for this task. Here is the solution.





 gci | Rename-Item -NewName {$_.Name -replace '([0-9]{5}) (.*)','$1$2'}



No comments: