Replacing and trimming characters in filenames using a batch file (windoze)

Good morning,

I'm trying to rename some mp3 files and would like to achieve two goals:

1.  I want to replace underscores with spaces
2.  I want to strip off leading track numbers

For example I would like to take the following filename:

"01 - This_Is_The_Title.mp3"

And convert it to:

"01 - This Is The Title.mp3"

And then to:

"This Is The Title.mp3"

The biggest problem is with stripping numbers off because they are not consistent.  For example, I have files like the following:



01 - This_Is_The_Title.mp3

I could group similarly named files in directories without too much trouble to solve this and then hand sanitize the rest.

What do you think?
Who is Participating?
Steve KnightConnect With a Mentor IT ConsultancyCommented:
You want to do something like this.  Works on the current directory

@echo off
REM save this as renfiles.cmd or something
for /f "tokens=*" %%a in ('dir /b /a-d') do call :process "%%a"
goto :eof
set oldname=%~1
REM Replace and _ with space
set strippedname=%oldname:_= %
REM set newname to remove first 2 characters
set newname=%strippedname:~2%
echo rename "%oldname%" "%newname%"

In this case it strips specifically the first two characters -- if you can organise them like this.  Remove the echo command before the rename to actually have i do it once it looks OK (at the moment it just shows on the screen).

We could get more complex testing if each char is a number and removing it etc. but maybe this is enough for now?

ttist25Author Commented:
Hey Dragon,

Thanks for your response.  I think we're almost there.  

Is it possible to separate these into two batch files?  1 that removes the _ and one that strips the first characters?  

I think I would better be able to understand and modify the batch file that way.  

Also, it's renaming the batch file itself.  How would we call to a directory other than root?

Thanks again for your help!
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
No need for for /F,  a simple for is enough. Also separated in parts, and manage the different formats you gave:

@echo off
setlocal EnableDelayedExpansion
REM underscore to space
for %%F in (C:\MP3s\*_*.mp3) do (
  set old=%%~F
  ren "!old!" "!old:_= !"
REM remove track with minus
for %%F in (C:\MP3s\* - *.mp3) do (
  set old=%%~F
  ren "!old!" "!old:*- =!"
REM remove track without minus
for %%F in (C:\MP3s\0*.mp3 C:\MP3s\1*.mp3 C:\MP3s\2*.mp3) do (
  set old=%%~F
  ren "!old!" "!old:~2!"

Open in new window

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ttist25Author Commented:
Thanks for your response Qlemo it is greatly appreciated.

I get an error "The syntax of the command is incorrect" when I run this.

I'm using Vista if that makes any difference.
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Remove the first line (@echo off), and try again. You should see the command causing that error message.

Above line 10 needs to be corrected, I forgot to quote because of the spaces:
for %%F in ("C:\MP3s\* - *.mp3") do (

Steve KnightIT ConsultancyCommented:
Hi guys,  sorry for abondoing.  I offered my suggestions originally then wasn't online for the rest of the day then away for a weekend in Wales where the mobile internet signal is still steam driven 2G GPRS (which is better than no signal at all where I went last time I suppose...)

Anyway please let us know if you still need some more help and will be answer.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.