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

Posted on 2009-05-01
Last Modified: 2012-05-06
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?
Question by:ttist25
    LVL 43

    Accepted Solution

    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?

    LVL 1

    Author Comment

    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!
    LVL 67

    Assisted Solution

    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

    LVL 1

    Author Comment

    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.
    LVL 67

    Expert Comment

    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 (

    LVL 43

    Expert Comment

    by:Steve Knight
    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now