Well, here we are with error checking!
Why bother? Well, from my long ago days of DBase programming I'm still obsessive about sanitizing and checking input. Bad/unchecked input is how databases get corrupted or yield nonsensical answers, and its also how the bad guys are able to overflow buffers and inject code at runtime.
DOS/Windows environmental variables are untyped and string or arithmetical manipulations are limited, difficult and rather idiosyncratic but at last now the routine won't stall or go into an endless loop or produce unexpected output.
Rather appropriately I got my kitten to do fuzz testing by walking around on the keyboard and so far, so good - though I don't claim it is bulletproof.
-----------------------------------------------------------------------
@echo off
cls
goto start
rem makedirs.cmd
rem Version 0.01.10
rem Creates multiple sequentially numbered subdirectories
rem Copyright ajbc - ajbc{#at#}iafrica.com
rem This utility starts from the directory that it finds itself in so
rem put it in the directory where you want to create the new subdirectories
rem environmental variables used are numdir, counter and filename
:start
set /P numdirs=How many directories do you want to create?
rem get user input on number of directories to create
:chk_input
set /A number=%numdirs%
if %number% LEQ 0 goto error1
rem Catches most non-numeric input like "Fred" or "hotpants"
rem What is doesn't catch either causes an error2 or the batchfile simply exits
if %number% GTR 9999 goto error2
rem Catches numbers > 9999 and invalid input that error1 misses
:carry_on
echo.
echo Please wait a moment while the directories are created
set /A counter=0
set filename=0
rem counter is numeric (forced by /A) and filename is a string (default)
:loop
set /A counter=%counter%+1
if %counter% LEQ 9999 set filename=%counter%
if %counter% LEQ 999 set filename=0%counter%
if %counter% LEQ 99 set filename=00%counter%
if %counter% LEQ 9 set filename=000%counter%
md %filename%
rem check if the counter has reached numdirs
if %counter% EQU %numdirs% goto end
rem exit when numdirs is reached
goto loop
:error1
cls
echo Invalid input!
pause
goto start
:error2
cls
echo Invalid input or exceeds 9999 subdirectory limit!
pause
goto start
:end
rem tidy up (remove variables used from environment)
set numdirs=
set counter=
set filename=
rem end batch and exit
---------------------------------------------------------------------------
An old programmer's saying is that programs are never finished, merely abandoned!
Thank you x213a for taking my remaining little grey cells for a run (gasp! gasp!) and thank you Feline (yes, its bloody hot in Cape Town today!) and jimtherev for your kind words!
Mac
Last edited by Mac the Knife; 11th March 2010 at 11:30.