xarokk
xarokk

Reputation: 13

creating new files with different names in Batch

In the office we have a extense and well defined file structure , and i have to create a hundreds of directories with the same name but differing in the last numbers.

directory0001
directory0002
...
directory0324

this is what i have done:

SET B=0001
SET C=0324

:while1

IF NOT %B%==%C%
(
echo "first loop"
SET COUNTER=0
IF NOT %COUNTER%=1
(
echo "Secoond loop"
mkdir "C:\pathfile\directory00"%B%
SET COUNTER==1
)
else() 
SET B=%B%+1
goto :while1
)
else
(
)

I'm not sure whether i'm using properly the operators or not, i'm using what i've found in different posts.

-i'm using windows terminal to debugg the code, there is a better way?

Upvotes: 0

Views: 117

Answers (1)

user1016274
user1016274

Reputation: 4209

There is a FOR loop for exactly this, incrementing a count from one value to the end value:

for /L %%i in (1,1,324) do if %%i LEQ 9 ( md "C:\pathfile\directory000%i" ) else if %%i LEQ 99 ( md "C:\pathfile\directory00%i" ) else ( md "C:\pathfile\directory0%%i" )

This will start at 1, increment by 1, until 324 is reached. The IF statement is only needed for formatting leading zeroes.
Edit: this is the complete code with proper indentation so that you (as a novice) can understand the flow more easily:

@echo off
SETLOCAL ENABLEEXTENSIONS

REM enable cmd extensions so that mkdir/md will create all intermediate folders

SET first=1
SET last=324

REM numeric extension will be appended with 4 places to this foldername
SET folder=C:\users\goofy\manydirs\directory

FOR /L %%i in (%first%,1,%last%) DO (
    IF %%i LEQ 9 (
        mkdir %folder%000%%i
    ) ELSE IF %%i LEQ 99 (
        mkdir %folder%00%%i
    ) ELSE IF %%i LEQ 999 (
        mkdir %folder%0%%i
    ) ELSE (
        mkdir %folder%%%i
    )
)

Upvotes: 1

Related Questions