Renaming and sorting

September 6, 2009 at 03:22:51
Specs: Windows XP
Hi guys.
I need some help in batch scripting.
I have to rename a group o files into capx.mp3 with x=01,02,..,09,10,11,...,n, where n is the number of files in the directory.
I used this batch

--------

@echo off

setLocal EnableDelayedExpansion

:ricom
cls

echo.
set /p cartella=Inserisci la cartella: D:\Downloads\Radio3 - Il terzo anello - Ad alta voce\
cd "D:\Downloads\Radio3 - Il terzo anello - Ad alta voce\%cartella%"

dir /b *.mp3> lista.txt

echo.
echo Lista dei files da rinominare
echo.
type lista.txt
echo.
echo.
set /p scelta=Continuo? 

if "%scelta%"=="s" (goto avanti) else goto cont

:avanti

set /A b=1

for /F "tokens=1 delims=ΒΆ" %%a in (lista.txt) do (
set c=%%a
call :pass2
)
goto cont

:pass2
if "%b%" LEQ "9" (ren "%CD%\%c%" cap0%b%.mp3) else ren "%CD%\%c%" cap%b%.mp3
set /A b+=1
goto :eof

:cont

del lista.txt

set /p again=Ricominciamo? 

if "%again%"=="s" (goto ricom) else exit

--------

(I know that this horrify most of you, but this is what I was able to do...)

The problem is that the original file are often named filex.mp3 with x=1,2,...,9,10,11,..,n so, when I read the content of "lista.txt" the files are sorted in such a way:

file1.mp3
file10.mp3
...
file2.mp3
file20.mp3
...

I can't sort them by date because I didn't create them in that order.
How can I hit the mark?
Thank you in advice ;)

Gaetano

PS: Sorry for the bad english: I'm Italian


See More: Renaming and sorting

Report •


#1
September 6, 2009 at 22:44:22
Uf the file names areconsistent in layout you might make a routine to accomodate the filex.mp3 names.

Try posting a few real file names.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
September 7, 2009 at 00:27:09
...>dir/b/OD
unaquestioneprivata1.mp3
unaquestioneprivata2.mp3
unaquestioneprivata3.mp3
unaquestioneprivata4.mp3
unaquestioneprivata6.mp3
unaquestioneprivata7.mp3
unaquestioneprivata8.mp3
unaquestioneprivata9.mp3
unaquestioneprivata10.mp3
unaquestioneprivata11.mp3
unaquestioneprivata12.mp3
unaquestioneprivata13.mp3
unaquestioneprivata14.mp3
unaquestioneprivata15.mp3
unaquestioneprivata16.mp3
unaquestioneprivata18.mp3
unaquestioneprivata19.mp3
unaquestioneprivata20.mp3
unaquestioneprivata21.mp3
unaquestioneprivata5.mp3
unaquestioneprivata17.mp3

...>dir/b/ON
unaquestioneprivata1.mp3
unaquestioneprivata10.mp3
unaquestioneprivata11.mp3
unaquestioneprivata12.mp3
unaquestioneprivata13.mp3
unaquestioneprivata14.mp3
unaquestioneprivata15.mp3
unaquestioneprivata16.mp3
unaquestioneprivata17.mp3
unaquestioneprivata18.mp3
unaquestioneprivata19.mp3
unaquestioneprivata2.mp3
unaquestioneprivata20.mp3
unaquestioneprivata21.mp3
unaquestioneprivata3.mp3
unaquestioneprivata4.mp3
unaquestioneprivata5.mp3
unaquestioneprivata6.mp3
unaquestioneprivata7.mp3
unaquestioneprivata8.mp3
unaquestioneprivata9.mp3

...>

There are 73 directory and 1633 named like those. The common pattern is <title><progressive number>.mp3.
The files #5 and #17 are at the end of the list because my conversion software give me an error and I had to reprocessate those files.
I have to rename them because my mp3 player can't read them in the correct order. I have to do that by hand? Hoping not...
Gaetano


Report •

#3
September 7, 2009 at 00:29:44
Obviously the filenames haven't a common lenght: they are a list of audiobooks ^_^

Report •

Related Solutions

#4
September 7, 2009 at 18:35:30
I suppose what you need is to rename a list like that:
music1
music20
music440
music1222

to get this:
music0001
music0020
music0440
music1222

In this case all your musics have the same principal name, differing only in a final number. It would be uncommon, but this bat might do the work:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('dir /b *.mp3') do (
set nfile=%%~na
set ffile=%%~fa
set /a ct=0
set numb=
call :strip
rename "!ffile!" "!newfile!.mp3"
)
goto :eof
:strip
set /a ct=%ct%-1
echo arquivo: %xfile%
set x=!nfile:~%ct%!
set x=!x:~0,1!
for %%j in (0,1,2,3,4,5,6,7,8,9) do (
if "%%j"=="%x%" (
set numb=%x%%numb%
goto :strip
)
)
set newfile=!nfile:%numb%=!
set /a numb=%numb%
if %numb% lss 10 set newfile=%newfile%000%numb% & goto :eof
if %numb% lss 100 set newfile=%newfile%00%numb% & goto :eof
if %numb% lss 1000 set newfile=%newfile%0%numb% & goto :eof
goto: eof

------------------------------------------------

If the musics have diferent names and numbers, you would have to change
this format: if %numb% lss 10 set newfile=%newfile%000%numb% & goto :eof
to this: if %numb% lss 10 set newfile=000%numb%_%newfile% & goto :eof

Then your files would seem like that:
0008_baby please dont go.mp3
0404_I will be a dog.mp3
etc


Report •

#5
September 8, 2009 at 00:42:21
I've tried that, but it doesn't seem to work :(
Running it without "@echo off" it seems to fail when define the variable numb, but I can't be sure because I can't understand the code you've kindly posted here.

I'm not sure I was clear enough: I have a set of 73 directory with a number of files varing from 10 to 50 for each one. Those files are named with a variable text (the title) and a progressive number (from 1 to 10 or 1 to 50, depending on the number of files in the current dir). The filenames doesn't contain any space or strange character. The pattern is <name><number>.mp3.

I need to rename them in cap01.mp3, cap02.mp3, ..., cap13.mp3 and so on in each dir, i.e. \a\cap01.mp3...; \b\cap01.mp3...

Thank you for your effort, however ^_^

Gaetano

PS: Can you give me a simple reference to understand the sintax "set x=!nfile:~%ct%!"?


Report •

#6
September 8, 2009 at 01:39:55
I've modified your code in such a way:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('dir /b *.mp3') do (
set nfile=%%~na
set ffile=%%~fa
set /a ct=0
set numb=
call :strip
rename "!ffile!" "!newfile!.mp3"
)
goto :eof
:strip
set /a ct=%ct%-1
echo arquivo: %xfile%
set x=!nfile:~%ct%!
set x=!x:~0,1!
for %%j in (0,1,2,3,4,5,6,7,8,9) do (
set /a var=%%j
call :prova
)
set newfile=!nfile:%numb%=!
set /a numb=%numb%
if %numb% lss 10 set newfile=%newfile%0%numb% & goto :eof
goto: eof

:prova
if "%var%"=="%x%" (
set numb=%x%%numb%
goto :strip
)

Now it rename all my files, but they are in the wrong order. Moreover the file that may be "name10.mp3" is renamed in "name.mp3", without the number.
Finally I can't obtain that the filename is capXX.mp3 and not <originalname>XX.mp3.

It was a great improvement, however. If you can link a me something to study the sintax "set x=!nfile:~%ct%!" it would be great! I've just learned something about the modifiers and I don't want to stop!

Thanks a lot!

Gaetano


Report •

#7
September 8, 2009 at 04:13:33
Now I understand your need. It was so simple.

@echo off
setlocal enabledelayedexpansion
set /a numb=0
for /f "tokens=* delims=" %%a in ('dir /b *.mp3') do (
set /a numb+=1
rename "%%a" "cap!numb!.mp3"
)

p.s_ If there are another audio formats unless MP3 in the folders, change "mp3" in the bat to "*" or to a list: dir /b *.mp3 *.ogg *.ram ...

_ if you want format cap0001 insted of cap1, see previous bat (if %numb% lss 10...).



Report •

#8
September 8, 2009 at 05:32:28
ehm... no.
First of all I need the file to be named cap01.mp3 and not cap1.mp3. The difference is that my mp3 player will reproduce them in the wrong order, that is
cap1
cap10
cap11
...
cap19
cap2
cap20
cap21
...

So I need the filename to be cap01.mp3. Obviously the tenth file has to be named cap10.mp3 and not cap010.mp3.

Second, if you take a look at "Response Number 2" of this post you will see that the command "dir" don't sort the file correctly, so I will rename i.e. the file "unaquestioneprivata5.mp3" into "cap20.mp3" instead of "cap05.mp3" and this would be very very annoying.

Thank you however for wasting your time with me :)

Gaetano


Report •

#9
September 8, 2009 at 11:16:24
The solution can be achieved by combining the 2 bats I posted.
But I did a fatal error in the first bat: I write "goto: eof". It should be: goto :eof. This litle diference is fatal. Its fixed now.

I combined the 2 bats in one. It will do the work that dir command can't do for himself and rename files to capsxxxx format. This third and fourth 'x' will prevent problems in case some day you have folders with more than 99 or 999 files. If you want xx format, you may change the bat.
I tested and it worked.

echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('dir /b *.mp3') do (
set nfile=%%~na
set ffile=%%~fa
set /a ct=0
set numb=
call :strip
cls
rename "!ffile!" "!newfile!.mp3"
)
goto :eof
:strip
set /a ct=%ct%-1
set x=!nfile:~%ct%!
set x=!x:~0,1!
for %%j in (0,1,2,3,4,5,6,7,8,9) do (
if "%%j"=="%x%" (
set numb=%x%%numb%
goto :strip
)
)
set newfile=!nfile:%numb%=!
set /a numb=%numb%
if %numb% lss 10 set newfile=cap000%numb%&goto :eof
if %numb% lss 100 set newfile=cap00%numb%&goto :eof
if %numb% lss 1000 set newfile=cap0%numb%&goto :eof
goto :eof


Report •

#10
September 8, 2009 at 12:23:14
You are G R E A T!
It work like a charm!!!
I'm admired!
Thank you very much!

Gaetano


Report •

#11
September 9, 2009 at 05:50:18
TOPIC SYNOPSIS

Ok Gaetano, job done. However I will make a synopsis of this topic in order to be useful to others. Most people will not read each piece of a long post like that to find the useful part.
So, lets resume the topic (to begin, be complacent with my English).

Gaetano has some folders with mp3 files. Filename format is <title><number>.
But his mp3 player queues the audio files in a Windows98 style:
x1
x2
x20
..
x29
x3
x30
...

To solve this, the numeric part of filenames have to be converted into 000n format, for instance:
I cant let you go 0003 , my life is in your hands 0050 , come back to me 0800.
(Always a 4 digits number if any number in filenames exceeds 9999)

The following bat do this conversion, remembering that it is presumed this specific file format: <title><numer>, without any number in title part.
_____________________________________________________

@echo off&setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('dir /b *.mp3') do (
set nfile=%%~na
set ffile=%%~fa
set /a ct=0
set numb=
call :strip
cls
rename "!ffile!" "!newfile!.mp3"
)
goto :eof
:strip
set /a ct=%ct%-1
set x=!nfile:~%ct%!
set x=!x:~0,1!
for %%j in (0,1,2,3,4,5,6,7,8,9) do (
if "%%j"=="%x%" (
set numb=%x%%numb%
goto :strip
)
)
set newfile=!nfile:%numb%=!
set /a numb=%numb%
if %numb% lss 10 set newfile=%newfile%000%numb%&goto :eof
if %numb% lss 100 set newfile=%newfile%00%numb%&goto :eof
if %numb% lss 1000 set newfile=%newfile%0%numb%&goto :eof
goto :eof

------------------

To convert into a <number><title> format its only to do a litle change in 3 last lines of the bat, before 'goto :eof'.

The final solution to Gaetano is in Response n.9, but it's a bit specific to his own case.


Report •


Ask Question