1

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"

Eli Richardson
  • 934
  • 7
  • 25
  • 1
    To see the errors simply remove `@echo off` and run the batch file manually from the Command Prompt console. – wOxxOm Nov 29 '15 at 05:25
  • 1
    @EliR See the answers on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/a/26388460/3074564) and [Why is my cd %myVar% being ignored?](http://stackoverflow.com/a/33484400/3074564) – Mofi Nov 29 '15 at 11:10

2 Answers2

0

Use

set variableTrue=1

rather than

set variableTrue EQU 1

This batch script:

@echo off
setlocal enabledelayedexpansion
cls
set variableTrue=1

echo A
if %variableTrue% EQU 1 goto next
echo B
goto :EOF

:next
echo C

outputs

A
C
Mofi
  • 46,139
  • 17
  • 80
  • 143
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
  • It still gives me the same error. Is it because the "set" and "if" statements are too far apart? Your code, however worked fine. – Eli Richardson Nov 29 '15 at 05:02
  • 2
    @EliR: Without seeing the *actual* code, we can't tell. Note however that batch is often sensitive to spaces, so `set variableTrue = 1` (if that is what you coded) would set a variable named "variabletrue[space]" to "[space]1" .`if %v% equ 1...` (where v has not been set) will yield a syntax error since it gets executed as `if equ 1...` and "1" is not one of the tokens that it expects as a comparator. The `equ` in this case is a legitimate string. To pause a batch to see messages, use the `pause` command or run your batch from the prompt (Programs>accessories>comand prompt) – Magoo Nov 29 '15 at 07:28
  • @Magoo I edited the original question to add an example of why it's not working. – Eli Richardson Nov 29 '15 at 19:23
0

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
dbenham
  • 127,446
  • 28
  • 251
  • 390