Iterniam
Iterniam

Reputation: 737

Label loop crashes seemingly without reason

Don't ask me why, but I try to create a 5-digit code consisting of folders. The following code seemed promising, however at folder 0\0\1\0 it crashes: it returns that 0\0\1\0\%E% already exists. Definition for %E% can be found in the code.

@echo off
set A=0
set B=0
set C=0
set D=0
set E=-1

:A

set /a E=E+1
if %E%==10 (set E=0 && set /a D=D+1)
if %D%==10 (set D=0 && set /a C=C+1)
if %C%==10 (set C=0 && set /a B=B+1)
if %B%==10 (set B=0 && set /a A=A+1)
if %A%==10 goto B
mkdir %A%\%B%\%C%\%D%\%E%
REM pause to get the error
goto A

:B
echo 111.111 folders have been made
pause
exit

Looking for a fix or a new version alltogether.

Upvotes: 2

Views: 222

Answers (3)

Aacini
Aacini

Reputation: 67216

The Batch file below is an equivalent, but simpler solution:

@echo off

set ABCDE=99999

:loop
   set /A ABCDE+=1
   mkdir %ABCDE:~1,1%\%ABCDE:~2,1%\%ABCDE:~3,1%\%ABCDE:~4,1%\%ABCDE:~5,1%
if %ABCDE% neq 199999 goto loop

echo 111.111 folders have been made
pause

Upvotes: 2

aschipfl
aschipfl

Reputation: 34909

@JosefStyons identified the core problem in his answer: there are spaces after the numerical digits in the values; but this is actually true for all variables except A.
This is caused by the if command lines -- let us pick one as an example:

if %E%==10 (set E=0 && set /a D=D+1)

The reason for the problem is the space after the set E=0 command. It is also the same for the other if lines of course.

To solve that issue, you could do either:

if %E%==10 (set "E=0" && set /a D=D+1)

or:

if %E%==10 ((set E=0) && set /a D=D+1)

or even (but with degraded legibility):

if %E%==10 (set E=0&& set /a D=D+1)

The problem does not exist for set /A instances as they treat values as numeric ones, whereas set commands without the /A switch treat values as strings.

Upvotes: 2

JosephStyons
JosephStyons

Reputation: 58685

Your %D% is getting a trailing space after the first iteration, so the MKDIR statements end up like this about 100 folders in (note the spaces):

...
mkdir 0\0\0\9\7
mkdir 0\0\0\9\8
mkdir 0\0\0\9\9
mkdir 0\0\1\0 \0 <--space
mkdir 0\0\1\0 \1 <--space
mkdir 0\0\1\0 \2 <--space
mkdir 0\0\1\0 \3 <--space
mkdir 0\0\1\0 \4 <--space
mkdir 0\0\1\0 \5 <--space
mkdir 0\0\1\0 \6 <--space
mkdir 0\0\1\0 \7 <--space
mkdir 0\0\1\0 \8 <--space
mkdir 0\0\1\0 \9 <--space
mkdir 0\0\1\1\0 
mkdir 0\0\1\1\1
mkdir 0\0\1\1\2
mkdir 0\0\1\1\3
...

The evaluation stops at the space, and indeed "0\0\1\0" does already exist at that point.

I honestly don't know why that's only happening to D, and not the other variables. I'm probably missing something obvious.

You can suppress the problem by adding this line just before the "mkdir", however:

if "%D%"=="0 " set D=0

On another note, a helpful way to debug batch files like this one is to remove the "pause" statements, put an "echo" in front of the "mkdir", then just redirect the output to a file. You can look at the output and see what mkdir statements it would have executed.

Upvotes: 1

Related Questions