How to create a batch command to change file names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Geroido
Posts: 1
Joined: 01 Nov 2012 05:27

How to create a batch command to change file names

#1 Post by Geroido » 01 Nov 2012 05:36

Hi
I have a folder of 1000s of mp3 files with names in the following format:

02 Zenaida aurita.mp3
01 - Red-throated Loon.mp3

What I want to do is create a dos command that trawls through the folder and removes the 02, 01, the hyphen and initial space before the actual name so that I end up with just (Zenaida aurita.mp3). I want to do this for every file in the folder. Bear in mind that it is not just 01 OR 02. The files have any number at the beginning such as 099. However, I do not want any numbers at the end of the file name removed or hyphens in the middle of a name so some files are like this:

38 - Black-chinned Mountain-Tanager (2).mp3

and this one should end up looking like this:

Black-chinned Mountain-Tanager (2).mp3

Any help and ideas?

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to create a batch command to change file names

#2 Post by Aacini » 01 Nov 2012 08:30

Have ALL files a number at beginning? Or may be files with no number?

If all files haves numbers, this is the solution:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for %%a in (*.mp3) do (
   set "name=%%a"
   rem Remove first characters until first space
   set "name=!name:* =!"
   rem Remove first hypen and space, if any
   if "!name:~0,1!" equ "-" set "name=!name:* =!"
   ren "%%a" "!name!"
)


Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to create a batch command to change file names

#3 Post by foxidrive » 01 Nov 2012 21:52

He asked the same question elsewhere and I put this together. It handles ! characters but % could be an issue.

Code: Select all

@echo off
del renmp3.bat.txt 2>nul
for %%a in (*.mp3) do (
for /f "tokens=1,2,* delims= " %%b in ("%%a") do (
for /f "delims=0123456789" %%e in ("%%bA") do (
if "%%e"=="A" (
if "%%c"=="-" (
>>renmp3.bat.txt echo ren "%%a" "%%d"
) else (
>>renmp3.bat.txt echo ren "%%a" "%%c %%d"
)
)
)
)
)
echo check renmp3.bat.txt for issues, if it's ok rename it .bat and run it.
pause

Post Reply