1.

Solve : Find and replace file name?

Answer»

Hi
I have a problem I am trying to solve. I have many mp3 files (several hundred) in a folder with the following type names:

accipiter-gentilis.mp3
acrocephalus-arundinaceus.mp3
acrocephalus-palustris.mp3
carduelis-chloris.mp3

Let's say these files are stored in a folder location C:\documents\music

I need to scan through these files one by one, remove the hyphen and replace it with a space.
Got any ideas??

ThanksSave this batch script in the folder where the mp3 files are. Run it and if you are HAPPY with what it shows, edit it in Notepad to remove the ECHO command from the start of the renaming line.

Code: [Select]@echo off
setlocal enabledelayedexpansion
for %%A in (*-*.mp3) do (
set oldname=%%A
set newname=!oldname:-= !

REM remove the echo from the next line
REM when you are happy with what it proposes to do
Echo RENAME "!oldname!" "!newname!"

)
Echo Finished...
Echo.
pause

Hi Salmon Trout
Thank so much. That worked perfectly - exactly what I wanted. However I like to understand what is happening. Would you mind COMMENTING this so I can unstand what is occuring line for line??
Thanks

@echo off
setlocal enabledelayedexpansion
for %%A in (*-*.mp3) do (
set oldname=%%A
set newname=!oldname:-= !

REM remove the echo from the next line
REM when you are happy with what it proposes to do
RENAME "!oldname!" "!newname!"

)
Echo Finished...
Echo.
pauseQuote from: geroido on February 15, 2011, 12:05:01 PM

Would you mind commenting this

Code: [Select]@echo off

REM You need this if you want to set and then use
REM variables inside parenthetical structures such
REM as loops. Google "delayed expansion" for full
REM details
setlocal enabledelayedexpansion

REM This is a loop in which...
REM for each file SPECIFIED by the mask *-*.mp3
REM %%A will hold each filename in turn
for %%A in (*-*.mp3) do (

REM put that name into a variable
set oldname=%%A

REM Create new name
REM MAKE a new variable holding
REM the old name replacing any hyphen
REM with a space
REM note use of exclamation marks instead
REM of percent signs with delayed expansion
set newname=!oldname:-= !

REM rename the current file with
REM the computed new name
RENAME "!oldname!" "!newname!"

)
REM loop end

pause

Thanks Salmon Trout
Very well explained. Saved me so much work.


Discussion

No Comment Found