# Solved: Yet another request for an mp3 batch command



## ttx336 (Nov 7, 2012)

hello all, I am brand new here... I have searched around and seen several batch command files for renaming mp3 files but am not intelligent enough as of yet to tweak them for my task at hand.

I would like to change from this format:

The Band Name - Title of Song One.mp3
The Band Name - Title of Song Two.mp3
The Band Name - Title of Song Three.mp3


to:

01 Title of Song One.mp3
02 Title of Song Two.mp3
03 Title of Song Three.mp3

and if the solution exists already I apologize in advance, I have not seen it.

In addition, if it wouldn't be too much trouble, a little explanation of the delimiters and parameters would really help me a lot.

Thanks in advance,
Gary


----------



## valis (Sep 24, 2004)

may want to check out bulk rename utility at http://download.cnet.com/Bulk-Rename-Utility/3000-2248_4-11434534.html. It's what I've used for years to rename tons of mp3 files organized to my particular selections.

and welcome to TSG.


----------



## ttx336 (Nov 7, 2012)

thank you, I will check it out...

however, I do still need the education as I run across this issue at work quite often as well, we use a very old CNC programming language called Bravo, it is a variant of CompactII/NicamIV and it is bound to the old DOS 8.3 file naming system. Consequently, I often have to root around with grep, sed, etc... and understanding the batch programming helps tremendously. So far I have been successful in getting things done, but usually it is by sheer effort and trial-and-error method.

-G


----------



## valis (Sep 24, 2004)

I can't help with batch files, but pretty sure we have a few people here who can assist.


----------



## ttx336 (Nov 7, 2012)

valis said:


> I can't help with batch files, but pretty sure we have a few people here who can assist.


You most certainly do! I have seen some amazing, but as of yet baffling, scripts and batch files on here. I hope that I can get a better handle on this subject... I used to program in C and Pascal and dabbled in Assembler so I should be able to learn this, I am just really fuzzy on the delimiters and the issue of the use of SET for the tempstr, I think that is my main confusion right now...

-G


----------



## foxidrive (Oct 20, 2012)

Is this to change one folder of an album of the band, or a folder with a large number of mp3 files?


----------



## foxidrive (Oct 20, 2012)

Second question - do you have bands like AC-DC which have a - character in the name of the band?

Third question - the songs will be in alphabetical order and not track order, you realise this right?


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> Is this to change one folder of an album of the band, or a folder with a large number of mp3 files?


a folder with a large number of files, possibly even to recursively step through a number of folders, but that is not strictly required, it would just be icing on the cake, but certainly I do need to change many files.

Thanks,

-G


----------



## foxidrive (Oct 20, 2012)

In case you missed it, there are more questions above.


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> In case you missed it, there are more questions above.


sorry, apparently I replied while you were typing the second post... I suppose I should try to allow for the possibility of the names having a hyphen in them... they often do...

also, good point about the song order being alphabetical, I had not realized that... perhaps I should just strip off the band name and the first hyphen, but, for my edification, I would like to see, if it is possible that is, how it might be done to add the 01, 02, 03, etc...


----------



## foxidrive (Oct 20, 2012)

Clarification of the task is needed too:

Do you want to simply remove "the band name - " from every track and add an incrementing number? 

So for 99 files in a folder they will go from "01 title one.mp3" to "99 title xyz.mp3"

And do you want the tracks sorted by alphabet? 

So for 99 files in a folder they will go from "01 A title.mp3" to "99 Zed title.mp3"


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> Clarification of the task is needed too:
> 
> Do you want to simply remove "the band name - " from every track and add an incrementing number?
> 
> ...


regarding the incrementing number, yes, that is correct, start at:

"01 A title.mp3"

end at:

"99 Zed title.mp3"

(however many files are in the directory)

regarding the Alpha-Sort, it probably doesn't really matter, I don't know that they are in the proper order for the album anyway, but it is much easier to change them once there are two digits there as a placeholder, and, they could possibly be in the proper order so I don't think it is necessary to sort them alphabetically, I would be quite satisfied to just strip the string off that I specify, eg: "Band Name - " and replace with 01, 02, etc...


----------



## foxidrive (Oct 20, 2012)

I'm still asking questions. I know some pitfalls that can break this kind of thing and the task needs to be clear before starting.

Does the folder contain

abba - track one.mp3
abba - track two.mp3
AC-DC - track three.mp3
AC-DC - track four.mp3

and you want this

01 track one.mp3
02 track two.mp3
01 track three.mp3
02 track four.mp3

or is there only one band name in each folder?

Are there any bands with names that have " - " in the band name itself? 
Like "The Four - Seasons - track one.mp3"


----------



## foxidrive (Oct 20, 2012)

ttx336 said:


> I would be quite satisfied to just strip the string off that I specify, eg: "Band Name - " and replace with 01, 02, etc...


Hmmm. Do you want to *specify* the leading text to strip off in each folder?


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> I'm still asking questions. I know some pitfalls that can break this kind of thing and the task needs to be clear before starting.
> 
> Does the folder contain
> 
> ...


the folders are segregated by band name, so that should make it easier, and I don't believe there are any names with hyphens in them... if there are I think I could handle that with a single rename on the command line with something like:

ren "The Four - Seasons*.*" "The Four Seasons*.*"

Could I not?

Thanks so much for your effort and patience!

G


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> Hmmm. Do you want to *specify* the leading text to strip off in each folder?


yes, but my previous reply should make that easier as I would be doing this, quite likely, folder by folder, for a certain band, etc...


----------



## foxidrive (Oct 20, 2012)

ok. Here's something that might suit you:

It is safe to run and will only echo the REN command for each file. Remove the echo and the pause to make it work for real.

Execute the batch file like this: renmp3 !

Do not run it twice in a folder.

Scenario:

Folders have fewer than 100 MP3 files.
Folders have band names that do not have " - " in them.
Filenames will retain the same sorting as they currently have.

The leading "text - " will be stripped off each filename and be replaced with an incrementing 2 digit and zero-padded number.


```
@echo off
:: removes the leading "name - " from a filename.  

:: DON'T run it twice accidentally.

:: requires a ! after the batch file name to make it work.

if not %1.==!. goto :EOF
set c=0
for /f "delims=" %%a in ('dir *.mp3 /a:-d /o:n /b') do call :next "%%a"
GOTO:EOF
:next
set /a c=c+1
set num=00%c%
set num=%num:~-2%
set "name=%~nx1"
set "name=%name:* - =%"
echo ren %1 "%num% %name%"
pause
```


----------



## foxidrive (Oct 20, 2012)

Filenames with % and ^ could be problematic.


----------



## ttx336 (Nov 7, 2012)

I must copy and paste the code into a file named renmp3.bat, correct?

I am not clear about: Execute the batch file like this: renmp3 !

was the ! at the end a mistake? if not, then I need some guidance...

I see the ! in the script, but when I try:

renmp3 ! I get and error saying that the syntax of the command is incorrect.


----------



## foxidrive (Oct 20, 2012)

yes, copy the code into renmp3.bat

A second batch file can run it through all the folders in a tree:

Put this batch file (call it renmp3folders.bat) in the folder in which you want to recurse through every folder underneath. Put renmp3.bat in c:\

Then you can click on this renmp3folders.bat batch file

Keep pressing space until you are satisfied when checking the names - then quit, and remove the echo and pause from renmp3.bat and run this renmp3folders.bat batch file again to perform the renames.

There is no UNDO so be sure the names are the way you want them renamed.


```
@echo off
for /f "delims=" %%a in ( 'dir /ad /b /s' ) do (
pushd "%%a"
call "c:\renmp3" !
popd
)
```


----------



## ttx336 (Nov 7, 2012)

ttx336 said:


> I must copy and paste the code into a file named renmp3.bat, correct?
> 
> I am not clear about: Execute the batch file like this: renmp3 !
> 
> ...


I think I have it sorted... apparently it must be named renmp3.bat? I had named it renamemp3.bat and it won't work like that... why is that? renaming it to renmp3.bat executes just fine...

(of course I was calling it with renamemp3 ! while I had it named renammp3.bat)


----------



## foxidrive (Oct 20, 2012)

rename is an internal command - the actual name isn't hard coded in.


----------



## ttx336 (Nov 7, 2012)

ttx336 said:


> I think I have it sorted... apparently it must be named renmp3.bat? I had named it renamemp3.bat and it won't work like that... why is that? renaming it to renmp3.bat executes just fine...
> 
> (of course I was calling it with renamemp3 ! while I had it named renammp3.bat)


I guess I was being a dope in some fashion, it seems to work no matter the name, not sure what I was doing wrong...

It seems to work great! I will study it a while and ask some questions if you don't mind educating me.


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> rename is an internal command - the actual name isn't hard coded in.


I didn't see if hard-coded in, and my filename was renamemp3.bat, at any rate, it is back to that name and working now, not sure what silly mistake I was making...


----------



## foxidrive (Oct 20, 2012)

In the flurry of posts - check that you saw the second batch file above to process an entire tree.


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> In the flurry of posts - check that you saw the second batch file above to process an entire tree.


I did, thank you so much! I ran it live just now on a directory and it did it EXACTLY right! You are super!!

Is it ok if I study this a while and ask a few questions later? if so, may I pm you or should I ask them in this thread?

-G


----------



## foxidrive (Oct 20, 2012)

Ask any questions here. Others may find them useful one day.


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> Ask any questions here. Others may find them useful one day.


absolutely true!

my first question is this:

regarding the

```
if not %1.==!.
```
would you please expound on this? I understand the "if" and the "not", of course, but what value does %1 pick up? is it getting loaded from the command line? and what about the ".==!." ? what is it's purpose?


----------



## foxidrive (Oct 20, 2012)

if not %1.==!. 

%1 is the first token on the command line following the batch file name. 
There can be %2 to %9 for the next tokens, as well as the batch file name itself in %0

In this case it is checking %1 with ! to see if it should do something. The . in the two comparisons are to prevent empty tokens from generating an error.

So at runtime it will see this when ! is supplied
if not !.==!. goto :EOF
else it will see this 
if not .==!. goto :EOF

and as . does not equal !. it will branch to the End Of File.

There are certain characters, and ways of dealing with quoted tokens that change part of the syntax, but for this case for a single ! it's fine.

! is a special character when delayed expansion is used so be aware of that - here it is just another character.


----------



## foxidrive (Oct 20, 2012)

In case you query the use of %1 later in the batch file - that is from the 

call :next "%%a"

part of the loop, where it calls the :next label and "%%a" (which has the name of a filename) is then referred to as the %1 and this new set overrides the replaceable parameters (%0 to %9) from the actual batch file command line.


----------



## ttx336 (Nov 7, 2012)

so it is used to ensure that the user really intended to run the batch file, then? kind of a safety?

what about the 

```
set /a c=c+1
set num=00%c%
set num=%num:~-2%
```
c has been set to zero and then incremented once the first time so it is equal to 1,

but what is the /a for? 
what does 00%c% do? 
and what does the %num:~-2% do? seems like maybe it is formatting?


----------



## foxidrive (Oct 20, 2012)

but what is the /a for?

It makes set do arithmetic

 what does 00%c% do? 

The number in %c% is a plain number. This adds two zeros to the front of it.

and what does the %num:~-2% do? seems like maybe it is formatting?

Yes, it takes the two leading zeros plus the number, and uses the two from the right hand end.

So 1 becomes 01 and 2 becomes 02 and 23 stays 23 etc. Similarly you can take the last three digits and get numbers from 001 to 999


----------



## foxidrive (Oct 20, 2012)

ttx336 said:


> so it is used to ensure that the user really intended to run the batch file, then? kind of a safety?


yes, precisely. if a batch file can be particularly destructive then it's good method to protect against it being clicked, or the name typed accidentally. (I have many batch files named with single or double characters for quick and easy use, and sometimes a typo can run one).


----------



## ttx336 (Nov 7, 2012)

ok, I see I have much to learn!

in the for loop, it seems that the output from 'dir *.mp3 /a:-d /o:n /b' is acted upon by the subroutine :next; the /f switch tells the for loop to work on a file right? does that output from the dir command become that "file" for the for loop to work on?


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> yes, precisely. if a batch file can be particularly destructive then it's good method to protect against it being clicked, or the name typed accidentally. (I have many batch files named with single or double characters for quick and easy use, and sometimes a typo can run one).


Aha!! Clicking on it won't run it! now I see the wisdom in that, it seemed useful before, but now I see the real value in it, it MUST be run from command line, I assume...

This is great stuff, thanks so very much, I have asked one other question that you are likely reading right now, after that, I will force myself to leave you be for a while!!

-Gary


----------



## foxidrive (Oct 20, 2012)

ttx336 said:


> in the for loop, it seems that the output from 'dir *.mp3 /a:-d /o:n /b' is acted upon by the subroutine :next


Yes, it passes the filenames one by one.



> ; the /f switch tells the for loop to work on a file right? does that output from the dir command become that "file" for the for loop to work on?


The /f for in do can read a text file, or a list/text provided by the output of any program (such as DIR), or it can parse a simple string to alter it.

The help from this is quite good to spell out how to use it:
FOR /?


----------

