# Solved: Batch File Help (Renaming Filenames)



## neilduk (May 20, 2009)

Hi Folks.

I'm looking for some help on creating a batch file to perform the following commands.

I want it to look in a the following directory : C:\Music\Rename
and only perform the following file rename/find+replace commands on .mp3 files

delete ) from filename
replace ( with _
replace - with _-_
replace _ with (a blank space)
make all filenames Proper Case
then replace the blank spaces with _

_I guess the delete command could also be a find/replace command which replaces the character with no character?
_
I've currently been using batch rename software to rename the files but it would be great if I had a .bat file which, with one click, would automatically rename the files in the directory to my requirements.

Your help would be much appreciated.

Thanks!


----------



## neilduk (May 20, 2009)

Hi Folks.

I'm looking for some help on creating a batch file to perform the following commands. 

I want it to look in a the following directory: C:\Music\Rename
and perform the following file rename/find+replace commands on all .mp3 files in the directory:

delete ) from filename
replace ( with _
replace - with _-_
replace _ with (a blank space)
make filename Proper Case
then replace the blank spaces with _

I've currently been using batch rename software to rename the files but it would be great if I had a .bat file which, with one click, would automatically rename the files in the directory to my requirements. 

Please note that some of the filenames might be long filenames. I'm not sure if this would be a problem or not?

Your help would be much appreciated.

Thanks!


----------



## Squashman (Apr 4, 2003)

Proper Case would be the really hard part. 
I guess I am not understanding why you want to replace an underscore with a blank and then replace the space with an under score again. Kind of self defeating.


----------



## Squashman (Apr 4, 2003)

```
@Echo Off & Setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=* delims=" %%A IN ('dir /a-d /b C:\Music\Rename\*.mp3') DO (
   SET _tmpstr=%%A
   SET _tmpstr=!_tmpstr:)=!
   SET _tmpstr=!_tmpstr:(=_!
   SET _tmpstr=!_tmpstr:-=_-_!
   SET _tmpstr=!_tmpstr:_= !
   SET _tmpstr=!_tmpstr: =_!
   rename %%A !_tmpstr!
   )
```
This is the basic gist of it. Will probably need to call a subroutine before the last set statement to do the Proper Casing. Will have to research that.


----------



## Squashman (Apr 4, 2003)

Looks like it would be easier to do in a vbscript to proper case it. I am no vbscript wizard but you could be able to call the vbscript and proper case it.

Here is some examples of doing that in vbscript.
http://www.asp101.com/samples/pcase.asp


----------



## Squashman (Apr 4, 2003)

Double post
http://forums.techguy.org/dos-pda-other/828516-batch-filename-rename-help.html


----------



## Squashman (Apr 4, 2003)

I guess it is not worth reinventing the wheel either.
http://llbest.com/RenameMP3Files.htm


----------



## neilduk (May 20, 2009)

Squashman said:


> Proper Case would be the really hard part.
> I guess I am not understanding why you want to replace an underscore with a blank and then replace the space with an under score again. Kind of self defeating.


only because I assumed you would have to have a blank space instead of an underscore for proper case to work.

many thanks for your help I'll give that a go.

however i've just discovered "batch file renamer command line" and it appears to have the proper case function etc


----------



## Squashman (Apr 4, 2003)

I know this doesn't cleaup all the stuff you want but looks like it is ideal just for Title Casing everything.
http://www.febooti.com/products/filetweak/members/case/


----------



## neilduk (May 20, 2009)

sorry, ie crashed on me when I tried to post this thread ... didn't think it had submitted, thus the double post.

thanks for that link, i'll check it out


----------



## Squashman (Apr 4, 2003)

The Rename MP3 Title Case program can be called from the the batch file. So you could call that program first to Proper Case everything, then have a second For Loop to delete and remove any of the additional character you want to remove.


----------



## devil_himself (Apr 7, 2007)

try testing this code


```
@echo off 
setlocal enableextensions enabledelayedexpansion 
set src=C:\Music\Rename
pushd "%src%"

set u=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set l=abcdefghijklmnopqrstuvwxyz

for /f "tokens=*" %%a in ('dir /b /a-d *.mp3') do (
  set "v=%%a"
  set "m=!v:~0,1!"

  for /l %%b in (0,1,25) do call :up !l:~%%b,1! !u:~%%b,1!
    set "v=!m!!v:~1!"
    set "v=!v:)=!"
    set "v=!v:(=_!"
    set "v=!v:-=_-_!"	
    ren "%%a" "!v!"
)

 goto :eof
	
 :up
 set "v=!v:_%1=_%2!"
 set "m=!m:%1=%2!"
```


----------



## TheOutcaste (Aug 8, 2007)

And another take. This will do multiple file types at once, converts multiple spaces to single spaces, and trims trailing spaces. It also makes sure everything is lowercase including the extension, then converts to Proper Case

```
@Echo Off
SetLocal EnableDelayedExpansion
Set _ScrFldr=C:\Music\Rename
PushD %_ScrFldr%
:: You can add other extensions to do multiple file types
For /F "Delims=" %%A In ('Dir /A-D /B *.mp3 *.wmv') Do (
:: The space after the = is needed so we can Proper Case the first word
Set _FileName= %%A
:: Remove (, replace ( with _, replace - with _-_ , replace _ with space, and a first pass at removing multiple spaces
For %%I In (")=" "(=_" "-=_-_" "_= " "  = ") Do Call Set "_FileName=%%_FileName:%%~I%%"
:: Now make it all lower case, and another pass at removing multiple spaces
For %%I In ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" "  = ") Do Call Set "_FileName=%%_FileName:%%~I%%"
:: Proper Case and again remove multiple spaces
For %%I In (" a= A" " b= B" " c= C" " d= D" " e= E" " f= F" " g= G" " h= H" " i= I" " j= J" " k= K" " l= L" " m= M" " n= N" " o= O" " p= P" " q= Q" " r= R" " s= S" " t= T" " u= U" " v= V" " w= W" " x= X" " y= Y" " z= Z" "  = ") Do Call Set "_FileName=%%_FileName:%%~I%%"
:: Last check for multiple spaces
Call :RmLoop
Set _FileName=!_FileName: =_!
:: Remove any trailing underscore
Set _FileName=!_FileName:_%%~xA=%%~xA!
Ren "%%A" "!_FileName:~1!"
)
PopD
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::
:RmLoop
Call Set "_FileName=%%_FileName:  = %%"
Echo !_FileName!|Findstr /C:"  ">Nul
If !ErrorLevel!==0 Goto RmLoop
```
HTH

Jerry


----------



## TheOutcaste (Aug 8, 2007)

WMP just started playing a song by AC-DC, and it dawned on me that all of the above would change AC-DC to Ac_-_Dc.
So if your collection includes songs by AC-DC and you have the group name in the Filename you can add one more line right before the Rename statement to fix that if needed:
*Set _FileName=!_FileName:Ac_-_Dc=AC-DC!*

Same for any other hyphenated names you don't want to have changed

Jerry


----------



## Squashman (Apr 4, 2003)

Sweet!


----------



## ghostdog74 (Dec 7, 2005)

@OP, you might want to use something other than batch. Eg Python

```
import os
root="c:\\"
filetypes=[".mpg",".wmv",".mp3"] # add as you wish
for r,d,f in os.walk(os.path.join(root,"test")):
    for files in f:
        if files[-4:] in filetypes:
            ofiles=files
            files=files.replace(")","").replace("(","_")
            files=files.replace("-","_-_")
            #if your proper case means you want to change the first character to uppercase
            filename_with_uppercase = files[0].upper()+files[1:]
            newfilename = os.path.join(r,filename_with_uppercase)            
            os.rename( os.path.join(r,ofiles), newfilename)
```
it will make your programming life easier.


----------



## neilduk (May 20, 2009)

many thanks for all the helpful replies folks. however I've found a solution that has done exactly what I've been looking for.

http://www.bulkrenameutility.co.uk/Command.php


----------

