Note: The file Default.txt contains one line with these three characters:1:X
@echo off
set r=0
:: For loop retrieves lines of text from the file Default.txt
FOR /F "tokens=1,2 delims=:" %%a IN (Default.txt) DO (
:: Each line is saved to a different variable.
set _%%a=%%b
set n=%%a
)
set ln=1
setlocal enabledelayedexpansion
:process
:: This loop processes all the lines in the text file.
set r=0
if "%n%" GTR "0" (
:len
:: This loop determines the length of each string.
if not "!_%ln%:~%r%,1!"=="" (
set /a r=%r%+1
goto len
)
:space
:: This loop adds spaces to each string so they will all be 39 characters in length.
if "%r%" LEQ "39" (
:: Note that there is a mandatory space at the end of the following line.
set _%ln%=!_%ln%!
set /a r=%r%+1
goto space
)
set /a n-=1
set /a ln+=1
goto process
) else (
endlocal
set _1=%_1%
)
echo %_1%]
pause >nul
When the script is run however, instead of adding 38 spaces, it only adds 3. By turning echo back on, I found the exact point where it exits the :space loop.
C:\>if "1" LEQ "39" (
set _1=!_1!
set /a r=1+1
goto space
)
C:\>if "2" LEQ "39" (
set _1=!_1!
set /a r=2+1
goto space
)
C:\>if "3" LEQ "39" (
set _1=!_1!
set /a r=3+1
goto space
)
Up to this point, everything is working as it should. Suddenly:
C:\>if "4" LEQ "39" (
set _1=!_1!
set /a r=4+1
goto space
)
For some reason, 4 is suddenly greater than 39, and it moves on to the next section instead of incrementing the variable and looping again like it should.
C:\>set /a n-=1
C:\>set /a ln+=1
C:\>goto process
And the program moves on and only 3 spaces are ever added to the variable. I have no idea what the problem is and would be grateful for any insight.