8

I have multiple .mp3 files in the catalogue and want to remove artist's name from their filenames. All the files' names contain string

"Adhesive Wombat - "

followed by the song's title. I want to remove (not replace) every occurence of that specific string in every filename in current folder.

Not to mention, I've searched for answer on this site and failed to find an elegant and satisfying solution.

This is my first encounter with PowerShell.

6
  • 1
    ...Show us the code you've tried so far.. Commented Mar 16, 2015 at 12:48
  • SO is not a code writing service. It is expected that you would show a little effort especially for something that would be considered a well documented request. Show some research effort and if you are still stuck update your question and we would be more then willing to help you. You are looking for Get-ChildItem and Rename-Item... maybe you would have to use For-EachObject Commented Mar 16, 2015 at 13:01
  • Followind code worked: Dir | ren -NewName { $_.Name -replace "Adhesive Wombat - ", "" } but there also were filenames that contained no space between words, that is "AdhesiveWombat". I'd like to remove both strings with one clear line of code, instead of executing 2 orders, one for each case. Commented Mar 16, 2015 at 13:25
  • Dir | ren -NewName { $_.Name -replace "Adhesive ?Wombat - ", "" } Commented Mar 16, 2015 at 13:32
  • @CB. That is what I would have answered. You might as well do it and add a small explanation to close this one off. Commented Mar 16, 2015 at 13:43

2 Answers 2

22

As you know what you have is really close.

Dir | ren -NewName { $_.Name -replace "Adhesive Wombat - ", "" }

You added a stipulation that "Adhesive Wombat" may or may not contain a space. -replace uses regular expressions so lets harness that and make that space optional using a ?. I am also going to show you the full cmdlet name as supposed to aliases so you are aware of what is happening behind the scenes. It's hard to see but I tried to highlight the ? in bold for it to "pop".

Get-ChildItem | Rename-Item -NewName {$_.Name -replace "Adhesive ?Wombat - ", "" }

So that will match both

"other text Adhesive Wombat - other text "
"other text AdhesiveWombat - other text "

Also, depending on how much white space there is you could use \s+ where you have space which will match all consecutive white space.

Sign up to request clarification or add additional context in comments.

3 Comments

nice "small explanation" ;) up one
@CB. I hate short little answers-> "Works because of [link]" .Feels like im doing a diservice.
@Matt Perfect solution. Renamed 1210 files in less than 10 seconds.
6

From the above answers, the quick working example for simply copy&paste, that worked on win 10 Powershell:

  1. change directory to your target directory
  2. define files with e.g. *.txt or set all files with *.*
  3. insert your Strings for replacement
cd C:\.... 
Get-ChildItem *.* | Rename-Item -NewName {$_.Name -replace "StringtoReplace", "newString"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.