vote up 0 vote down star
1

Hello, i have one big folder with many folders with some files, like this:

FOLDER
img 08-21
- japan.jpg; 
- german.jpg;
- london.jpg;

img 08-22
- caribic.jpg
- malta.jpg

ims 08-23
- center.jpg
- circle.jpg
- bike.jpg

and i want to rename by batch(.bat or whatever for winXP cmd) to

FOLDER
01-img 08-21
- 01-japan.jpg
- 01-german.jpg
- 01-london.jpg

02-img 08-22
- 02-caribic.jpg
- 02-malta.jpg

03-ims 08-23
- 03-center.jpg
- 03-circle.jpg
- 03-bike.jpg

I want to rename folder to prefix(number)-folder and files (in folder) to prefix(folder)-file. Can somebody pls help me?

flag

3 Answers

vote up 3 vote down

The following batch file should solve this. Explanations follow below.

@echo off
setlocal enableextensions enabledelayedexpansion
set counter=0
for /d %%d in (*) do (
    set /a counter+=1
    call :lz
    pushd %%d
    for %%f in (*) do ren "%%f" "!counter!-%%f"
    popd
    ren "%%d" "!counter!-%%d"
)
endlocal
goto :eof
:lz
set counter=000%counter%
rem adapt this to allow for three-digit numbering
set counter=%counter:~-2%
goto :eof

Basically this loops over all directories with

for /d %%d in (*)

The /d switch here is for looping only over directories. The current directory within the loop is stored in %%d. Then the counter (which is set to 0 at the start) is incremented by one and padded appropriately at the start with zeroes if necessary. This is done by the call to the :lz subroutine. Within that subroutine (it's at the end of the batch) the number of digits can be changed in the line

set counter=%counter:~-2%

by changing the 2 to 3 or so.

Then the directory will be changed to the one we're currently handling using pushd (which has the nice property that we can undo this directory change afterwards by using popd) and another loop is performed, this time over the files inside the directory:

for %%f in (*) do ren "%%f" "!counter!-%%f"

which renames the files and prepends the counter value to them. The exclamation marks around the counter variable name are so-called delayed expansion which is necessary here (why this is so can be read up in help set).

After renaming all files in the directory we jump out of it again and rename the directory itself. Nothing too fancy here.

link|flag
Nice answer. Good explanations too! – BlueNovember Jan 2 at 17:47
vote up 0 vote down

Do you have a specific problem with your batch file? Or are you just looking for someone to write one for you?

link|flag
It's not uncommon for people having problems to even start with batch files, though. – Johannes Rössel Jan 2 at 4:46
vote up 0 vote down

Thank you very much, it works. Thanks

link|flag
2  
welcome to superuser! this isn't a normal forum, so you should leave this as a comment on Johannes' answer (look for the grey "add comment" link). further, you can accept his answer by checking the little empty checkmark next to his answer. accept an answer isn't required, but it helps the rest of us see that your problem was solved. – ~quack Jan 2 at 13:15

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.