Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script to rename files

I have a bunch of files that have names that start with numbers, may contain spaces and dashes after one of two numbers, then sometimes (sometimes not) have a space just before the first alpha character of the name I want to keep, i.e.:

2-01 Dazed And Confused.txt (I want to rename this to Dazed And Confused.txt)

Or

02 - Uncle Salty.txt (I want to rename this to Uncle Salty.txt)

Or

02-The Night Before.txt (I want to rename to The Night Before.txt)

like image 535
user577651 Avatar asked Dec 27 '22 08:12

user577651


1 Answers

The Move-Item and Rename-Item cmdlets can accept the source directly from the pipeline and provide a new name using a script block:

dir | Move-Item -Destination { $_.Name -replace '^([0-9\-\s]*)' }

or

dir | Rename-Item -NewName { $_.Name -replace '^([0-9\-\s]*)' }
like image 154
Emperor XLII Avatar answered Dec 28 '22 23:12

Emperor XLII