How to use set and if statements in batch files

Eli Richardson

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"

dbenham

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use set and if statements in batch files

From Dev

How to use the if-else statements in batch programming?

From Dev

How to use nested FOR loops in batch files

From Dev

Set Variables in For, in Batch Files

From Dev

Set operations in batch files

From Dev

Batch- If statements based on files/websites open

From Dev

Batch, use wildcard in set

From Dev

For batch files, how to add a newline character to promptString when using "set"?

From Dev

Creating a mysql trigger for use with batch insert statements

From Dev

How do I use a batch file to recursively replace a string in files?

From Dev

how to best approach to use spring batch annotation or xml files ?

From Dev

spacebars: how to use and / or in if statements

From Dev

How to use If statements in a stream?

From Dev

How to use multiple if statements

From Dev

how to set variable on window batch file for private use only

From Dev

how to set variable on window batch file for private use only

From Dev

How to use nircmd.exe and set filename %~nF in batch for loop

From Dev

How to use batch command "where" and set a variable from a VBScript?

From Dev

How to batch organize files?

From Dev

How to batch organize files?

From Dev

Use drive letters on batch files

From Dev

Use Batch findstr for files with whitespaces

From Dev

How to use "if statements" in "let" statements in xquery

From Dev

How to use loop statements and condition statements in TOSCA?

From Dev

How to handle multiple 'for' statements in a batch file?

From Dev

How to send update batch with statements simultaneously in SQL

From Dev

How to handle multiple 'for' statements in a batch file?

From Dev

What alternatives to If-Else statements do batch files have?

From Dev

How to delete the list of files batch by batch?

Related Related

  1. 1

    How to use set and if statements in batch files

  2. 2

    How to use the if-else statements in batch programming?

  3. 3

    How to use nested FOR loops in batch files

  4. 4

    Set Variables in For, in Batch Files

  5. 5

    Set operations in batch files

  6. 6

    Batch- If statements based on files/websites open

  7. 7

    Batch, use wildcard in set

  8. 8

    For batch files, how to add a newline character to promptString when using "set"?

  9. 9

    Creating a mysql trigger for use with batch insert statements

  10. 10

    How do I use a batch file to recursively replace a string in files?

  11. 11

    how to best approach to use spring batch annotation or xml files ?

  12. 12

    spacebars: how to use and / or in if statements

  13. 13

    How to use If statements in a stream?

  14. 14

    How to use multiple if statements

  15. 15

    how to set variable on window batch file for private use only

  16. 16

    how to set variable on window batch file for private use only

  17. 17

    How to use nircmd.exe and set filename %~nF in batch for loop

  18. 18

    How to use batch command "where" and set a variable from a VBScript?

  19. 19

    How to batch organize files?

  20. 20

    How to batch organize files?

  21. 21

    Use drive letters on batch files

  22. 22

    Use Batch findstr for files with whitespaces

  23. 23

    How to use "if statements" in "let" statements in xquery

  24. 24

    How to use loop statements and condition statements in TOSCA?

  25. 25

    How to handle multiple 'for' statements in a batch file?

  26. 26

    How to send update batch with statements simultaneously in SQL

  27. 27

    How to handle multiple 'for' statements in a batch file?

  28. 28

    What alternatives to If-Else statements do batch files have?

  29. 29

    How to delete the list of files batch by batch?

HotTag

Archive