I suggest to read first debugging a batch file and second the answer on
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Next look on your rewritten code below:
:setup
@echo off
title Pokemon Shiny Sim
set "delay=0"
set "count=0"
set "chance=4096"
:settings
:setChance
cls
echo Set shiny chance (1 in x). Range: (1-1,048,567)
echo Leave blank for 1 in 4096.
set /P "chance=Input: "
set /A chance+=0
if %chance% GEQ 1 if %chance% LEQ 1048567 goto setDelay
echo Invalid input.
pause
goto setChance
:setDelay
cls
echo Set delay between attempts in seconds. Range: (1-60).
echo Leave blank for no delay.
set /P "delay=Input: "
set /A delay+=0
if %delay% == 0 goto loopStart
if %delay% GEQ 1 if %delay% LEQ 60 cls & goto loopStart
echo Invalid input.
pause
goto settings
:loopStart
set /A count+=1
set /A rand=%random% %% chance + 1
if %rand% == 1 (
echo Attempt: %count% ^| Shiny: Yes!
pause
) else (
echo Attempt: %count% ^| Shiny: No
)
goto loopStart
All spaces around the equal signs are removed in this batch code.
The command line set "delay = nul"
is modified to set "delay=0"
because the condition if %delay% == nul
is never true after execution of set /a delay = %delay%+0
resulting in execution of set /a delay = nul + 0
which results in assigning value 0
to environment variable delay
on nul
not existing as environment variable with that name having an integer value. The result of a valid arithmetic expression is always a number assigned as string to the environment variable and never a string like nul
.
set /a chance = %chance%+0
is modified to set /A chance+=0
and set /a delay = %delay%+0
is modified to set /A delay+=0
because otherwise the input check is insecure as the user has for example the freedom to enter |
for variable chance
resulting in execution of command line set /a chance = |+0
which cause an unexpected exit of batch file execution.
Never use %variable%
or !variable!
in an arithmetic expression as not needed in general.
The help output on several pages on running set /?
in a command prompt window explains in chapter about usage of set /A
that each string which can't be interpreted as number or operator is interpreted automatically as name of an environment variable whose current value should be converted to an integer on evaluation of the expression. If the environment variable is not defined at all or its value can't be successfully converted to a 32-bit signed integer, it is replaced in the expression by integer value 0
.
There are exceptions like the usage of a random number in an arithmetic expression which requires %random%
or !random!
or when a variable name contains a space character or a character which would be interpreted as operator. In such cases it is necessary that the Windows command interpreter replaces the environment variable name already in preprocessing state or immediately before execution of the command set
by random value respectively value of the environment variable.
set /a chance = %chance%+0
makes it also possible that the user of this batch file enters for example PROCESSOR_LEVEL
or PROCESSOR_REVISION
and this input although not being a number at all would be handled as valid because those two strings are the names of environment variables having numbers as values. PROCESSOR_REVISION
has by default a hexadecimal number assigned which can be processed nevertheless completely or partly as number by command set
.
Another syntax error is in block
if %rand% == 1 (
echo Attempt: %count% | Shiny: Yes!
pause
)
else (
echo Attempt: %count% | Shiny: No
)
The keyword else
must be on same line as the closing )
of true branch of the IF condition separated from )
with a space character.
And redirection operator |
must be escaped with caret character ^
to be interpreted as literal character to output into console window.
Note: set /A chance+=0
makes it still possible to enter for example 170 percent
or 170X
which results in chance
having value 170
and therefore input is valid although in real the entered string is not a number.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cls /?
echo /?
goto /?
if /?
pause /?
set /?
title /?