Solved How to generate a new folder with incremental double digits

March 18, 2015 at 09:39:33
Specs: Windows 7
Im in a need of a bat script that will create a folder and increment a double digit number in its name, in its current location every time i run it, starting from 01 to 99:
NewFolder01
NewFolder02

I found several batch scripts that create incremental folders but they all start with Name1 instead of Name01.
Thanks.

message edited by codescribble


See More: How to generate a new folder with incremental double digits

Report •


#1
March 18, 2015 at 20:26:32
✔ Best Answer
::====== script starts here ===============
::
:: scribble.bat 2015-03-19 10:07:11.51
@echo off & setLocal enableDELAYedeXpansioN

set/p DEST=where?
pushd !DEST!

for /L %%i in (1 1 99) do (
if %%i lss 10 (
set N=0%%i
) else (
set N=%%i
)
if not exist folder!N! (
md folder!N!
goto :donehere
)
)
:donehere
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#2
March 19, 2015 at 03:51:11
Hey awesome! There are some things i tried to change, first i took out the where destination question (so it works instantly in the folder), then i tried to make it check if the folder exists and add +1 to the number, but looks like it isn't working, im losing the "0"

::====== script starts here ===============
::
:: scribble.bat 2015-03-19 10:07:11.51
@echo off & setLocal enableDELAYedeXpansioN
for /L %%i in (1 1 99) do (
if %%i lss 10 (
set N=0%%i
) else (
set N=%%i
)
:loop
if exist folder!N! (
set /a N=%N% + 1
goto :loop
)
if not exist folder!N! (
md folder!N!
goto :donehere
)
)
:donehere
goto :eof
::====== script ends here =================


What did i do wrong? Thank you!

Edit:

I edited it more and now and its incrementing as it should but im getting triple digits instead of double digits :p

::====== script starts here ===============
::
:: scribble.bat 2015-03-19 10:07:11.51
@echo off & setLocal enableDELAYedeXpansioN
for /L %%i in (1 1 99) do (
if %%i lss 10 (
set N=0%%i
) else (
set N=%%i
)
:loop
if exist folder!N! (
set /a N=%N% + 1
goto :loop
)
if %%N lss 10 (
set N=0%!N!
)
if not exist folder!N! (
md folder!N!
goto :donehere
)
)
:donehere
goto :eof
::====== script ends here =================

message edited by codescribble


Report •

#3
March 19, 2015 at 06:06:52
I'm with you on leaving out the directory change.

I don't follow your logic in the rest. It works as I posted, sana pushd.

One caution:

%N% won't behave exactly like !N!, and often throws a curve ball.

=====================

M2 Get custom script or take private lessons


Report •

Related Solutions

#4
March 19, 2015 at 06:29:37
I'm so sorry you are right :), i accidentally edited the script in a wrong way while testing around and thought it didn't work!

Thank you so much :))

message edited by codescribble


Report •

Ask Question