1.

Solve : Batch file to copy and rename?

Answer»

I have a directory containing folders:
1
2
3
4
5
etc...

Inside each of these folders are files named:
Track01.mp3
Track02.mp3
etc...
(every folder starts at Track01)

I need a batch which will move all tracks into the root directory (the one containing the folders 1,2,3) and rename them Track01.mp3 ....Quote from: Linux711 on August 12, 2012, 11:48:36 AM

I need a batch which will move all tracks into the root directory (the one containing the folders 1,2,3) and rename them Track01.mp3 ....

You WANT to move them all into the root folder and give them all the same name? Track01.mp3? That won't work, but I don't think that's what you MEANT.

Does the ellipsis with an extra dot (those FOUR dots) imply some kind of sequence?




Quote
You want to move them all into the root folder and give them all the same name? Track01.mp3? That won't work, but I don't think that's what you meant.

No, I want to rename them otherwise I'd have 12 Track01s and so on because that's how many folders I have. However, I want them named with the same convention (i.e. Track01, Track02, etc...)

Quote
Does the ellipsis with an extra dot (those four dots) imply some kind of sequence?

Yeah, I just meant it would continue through all the tracks.

Thanks SALMON. I know you're really good at complex batch files. Do you think you can help?The first question is... the final filename order, how is decided? Do you want "1\track01.mp3" to be "Final track 01.mp3" in the root folder and number sequentially, taking the folders in order starting at 1 and the files in each folder in order? What will the final filename be? The filename prefixed with the original folder name e.g. 1-Track01.mp3 etc?

Do the subfolders start at 1 and go up without gaps to a final number?


Quote
The first question is... the final filename order, how is decided? Do you want "1\track01.mp3" to be "Final track 01.mp3" in the root folder and number sequentially, taking the folders in order starting at 1 and the files in each folder in order?

Yes, they will be taken sequentially, but not renamed for just the first folder. For subsequent folders, they will need to be renamed because the track numbers will continue to increase.

Quote
The filename prefixed with the original folder name e.g. 1-Track01.mp3 etc?

No prefix, that's what makes it hard. The numbers just keep increasing.

Quote
Do the subfolders start at 1 and go up without gaps to a final number?

Yes.

Here is a little more detail:

Basically, for the first folder all files can just be moved to the root without renaming. Let's say the first folder contains Track01 - Track20. When it moves the files in the second folder it renames Track01 to Track21 and so on. Then the numbers just increase until it has gone through all the folders.Quote from: Linux711 on August 12, 2012, 01:06:47 PM
Basically, for the first folder all files can just be moved to the root without renaming. Let's say the first folder contains Track01 - Track20. When it moves the files in the second folder it renames Track01 to Track21 and so on. Then the numbers just increase until it has gone through all the folders.

This makes it easier. Watch this space.
The subfolders run from 1 to what final number?

What is the highest number NN in TrackNN.mp3 that each folder might have?

Are all the mp3 files named TrackNN.mp3 where NN is a 2 digit number with a leading zero for 1 to 9?

Are there going to be more than 99 files in the root folder? More than 999? (You see where I am going here... ?)



Put this batch in the root folder.

Assuming there are less than 100 final files. Change the number of digits in the NEW file number like this:

set padnum=!padnum:~-2!

Change the 2 to the number required


Code: [Select]@echo off
setlocal enabledelayedexpansion
set fnum=1
set rootfolder=%~dp0
echo Root folder: %rootfolder%
for /f "delims=" %%A in ('dir /b /ad /on') do (
echo Folder %%A
for /f "delims=" %%B in ('dir /b /on "%%A\*.mp3"') do (
set padnum=00000000000!fnum!

REM set number of digits here
set padnum=!padnum:~-2!

REM Delete echo from the start of the next line when you are happy
echo Copy "%%A\%%~nxB" "%rootfolder%Track!padnum!.mp3"

REM Copy rather than move because it is safer. When you have run it and you are happy delete or move the subfolders

set /a fnum+=1

)
)

echo Done
Pause



Code: [Select]Root folder: C:\Batch\Test\After 04-07-2012\Sub-move-Rename\
Folder 1
Copy "1\1.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track01.mp3"
Copy "1\2.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track02.mp3"
Copy "1\3.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track03.mp3"
Copy "1\4.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track04.mp3"
Copy "1\5.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track05.mp3"
Copy "1\6.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track06.mp3"
Copy "1\7.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track07.mp3"
Copy "1\8.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track08.mp3"
Copy "1\9.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track09.mp3"
Folder 2
Copy "2\1.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track10.mp3"
Copy "2\2.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track11.mp3"
Copy "2\3.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track12.mp3"
Copy "2\4.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track13.mp3"
Copy "2\5.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track14.mp3"
Copy "2\6.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track15.mp3"
Copy "2\7.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track16.mp3"
Copy "2\8.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track17.mp3"
Copy "2\9.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track18.mp3"
Folder 3
Copy "3\1.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track19.mp3"
Copy "3\2.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track20.mp3"
Copy "3\3.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track21.mp3"
Copy "3\4.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track22.mp3"
Copy "3\5.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track23.mp3"
Copy "3\6.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track24.mp3"
Copy "3\7.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track25.mp3"
Copy "3\8.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track26.mp3"
Copy "3\9.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track27.mp3"
Folder 4
Copy "4\1.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track28.mp3"
Copy "4\2.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track29.mp3"
Copy "4\3.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track30.mp3"
Copy "4\4.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track31.mp3"
Copy "4\5.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track32.mp3"
Copy "4\6.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track33.mp3"
Copy "4\7.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track34.mp3"
Copy "4\8.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track35.mp3"
Copy "4\9.mp3" "C:\Batch\Test\After 04-07-2012\Sub-move-Rename\Track36.mp3"It works perfectly. The only annoying thing is the folders have to be named 01,02,03 otherwise it doesn't go in the right order with numbers above 9. Is there some code I can add that checks if the length of the folder name is less than 2 and then adds the zero and renames the folder?Quote
It works perfectly. The only annoying thing is the folders have to be named 01,02,03 otherwise it doesn't go in the right order with numbers above 9. Is there some code I can add that checks if the length of the folder name is less than 2 and then adds the zero and renames the folder?

I got it:

Code: [Select]for /f "delims=" %%A in ('dir /b /ad') do (
set foldername=%%A
set foldername=0!foldername!
set foldername=!foldername:~-2!
echo rename %%A !foldername!
)

The final:

Code: [Select]@echo off
setlocal enabledelayedexpansion
set fnum=1
set rootfolder=%~dp0
for /f "delims=" %%A in ('dir /b /ad') do (
set foldername=%%A
set foldername=0!foldername!
set foldername=!foldername:~-2!
rename %%A !foldername!
)
for /f "delims=" %%A in ('dir /b /ad /on') do (
for /f "delims=" %%B in ('dir /b /on "%%A\*.mp3"') do (
set padnum=00000000000!fnum!

REM set number of digits here
set padnum=!padnum:~-3!

REM Delete echo from the start of the next line when you are happy
move "%%A\%%~nxB" "%rootfolder%Track!padnum!.mp3"

REM Copy rather than move because it is safer. When you have run it and you are happy delete or move the subfolders

set /a fnum+=1

)
rd %%A
)

Thanks for the help, you're a genius.Yes, I was wondering about the sort order that Windows uses, where 10 is sorted before 2 etc. There is a registry setting, I believe, that can enforce either natural or numerical sort order in Explorer, but I am not sure whether it would apply to the command line DIR.

I must say, this is the type of thread that I really like, where the other person takes the ball and runs with it. Very well done.

Even PowerShell sorts the same way but the difference with PowerShell is you can pipe to the SORT command and use a regular expression to sort by so that it does sort numerically.


Discussion

No Comment Found