If statement variable problem
Posted: 21 Aug 2021 17:00
I'm a little baffled by this batch code here.
I have similar code structure before and outside of a conditional 'if' statement, but the rand2 value
inside the conditional is always seen as a zero value in the 'set hexa2=!HEXA:~%rand2%,2!' line.
What obvious thing have I overlooked here? The code structure seems straight-forward enough.
Thanks.
I have similar code structure before and outside of a conditional 'if' statement, but the rand2 value
inside the conditional is always seen as a zero value in the 'set hexa2=!HEXA:~%rand2%,2!' line.
Code: Select all
@echo off
cls
SETLOCAL ENABLEDELAYEDEXPANSION
set HEXA=1B1F2E303E474E4F575E5F6087909FA0B0D0D7DFE0F4
:THIS_WORKS
set /A rand1=%random% %% 22
set hexa1=!HEXA:~%rand1%,2!
color !hexa1!
pause
cls
set WONKY=ZZ
:THIS_DOES_NOT_WORK_AS_INTENDED
if "!WONKY!"=="ZZ" (
echo Inside the 'if' conditional. Good so far.
set /A rand2=%random% %% 22
echo rand2 is a new random number every time: !rand2! Good so far, but
echo rand2 is always interpreted as a '0' value in the following 'set hexa2=...' line
set hexa2=!HEXA:~%rand2%,2!
color !hexa2!
echo and color ^^!hexa2^^! always results in a screen / text color of '1B'
)
pause
:END
Thanks.