So i'm trying to make a small batch program with some sets and ifs, seems easy enough right? Apparently, you can't use "set" and "if" like this.
@echo off
setlocal enabledelayedexpansion
cls
set variableTrue EQU 1
Then continue the code and later in the program do this.
If %variableTrue% EQU 1 goto next
Please note I've tried it with exclamation marks as well. With the exclamation marks, it's like it completely ignores the statement, even if it's true it will continue as usual. With the percentage signs, it says very quickly before crashing "1" was not expected at this time. Or something like that, like I said it barely stayed for half a second. I always thought you could do it like this as long as there are no conflicting variables.
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ 13 goto thanks
If "!fn!" EQU 13 goto setvar
:setvar
set "coolestNum==1"
:thanks
cls
If "!coolestNum!"== 1 goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
That doesn't give an error, it just ignores the statement and keeps going as usual.
UPDATE: After fixing errors this still doesn't work. When I use exclamation marks it ignores the line, and when I use percentage signs it says: "
"1 was not expected at this time"
One set of problems is your IF statements. For example, If "!coolestNum!"== 1 goto cool
. The quotes are included in the comparison.
You need to be symmetric - either include quotes on both sides
If "!coolestNum!" == "1" goto cool
or neither:
If !coolestNum! == 1 goto cool
The same problem exists with If "!fn!" EQU 13 goto setvar
, as well as the line before it.
The other problem you have is set "coolestNum==1"
has an extra, unwanted =
. The second =
becomes part of the value. You only want two equal signs with IF comparisons.
Here is corrected code:
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ "13" goto thanks
If "!fn!" EQU "13" goto setvar
:setvar
set "coolestNum=1"
:thanks
set coolest
cls
If "!coolestNum!"=="1" goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
But your logic is needlessly convoluted. The following produces the exact same result.
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
echo What is your favorite number?
set /p fn=Favorite Number
if !fn! equ 13 (
set "coolestNum=1"
echo cool
) else echo Thanks
pause
exit /b
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments