bdhar
bdhar

Reputation: 23013

If condition in batch files

@echo off
SET var1="Yes"
SET var2="No"
SET var3="Yes"
if %var1%=="Yes"
    echo Var1 set
if %var2%=="Yes"
    echo Var2 set
if %var3%=="Yes"
    echo Var3 set

If I run the above script I get the following error. Can anyone pls help?

The syntax of the command is incorrect.

Thanks.

Upvotes: 30

Views: 133748

Answers (6)

Talibu Mammadov
Talibu Mammadov

Reputation: 1

set myvar=Yes
if %myvar%=="Yes" (
echo Myvar is yes
set myvar=nothing
)

or you can just use "&" like this:

set myvar=Yes
if %myvar%=="Yes" echo Myvar is yes&set myvar=nothing

Upvotes: 0

Ed999
Ed999

Reputation: 3091

Line-breaking is forbidden by the rules of syntax applicable in a command line script.

The command must instead be written to comply with the rules for splitting long commands into multiple lines -

if %var1%=="Yes"^
    echo Var1 set

In other words a caret (^) must be included, to tell the CMD program not to process the End Of Line flag.

Upvotes: 0

Nick
Nick

Reputation: 11

@echo off
setlocal enabledelayedexpansion

set var1=1
set var2=2
set var3=1

if "!var1!" == "1" echo Var1 set
if "!var2!" == "1" echo Var2 set
if "!var3!" == "1" echo Var3 set
pause

Upvotes: 1

David Webb
David Webb

Reputation: 193814

You can't put a newline like that in the middle of the IF. So you could do this:

if %var1%=="Yes" echo Var1 set

Or, if you do want your statements spread over multiple lines you can use brackets:

if %var1%=="Yes" (
   echo Var1 set
)

However, when you're using brackets be careful, because variable expansion might not behave as you expect. For example:

set myvar=orange

if 1==1 (
   set myvar=apple
   echo %myvar%
)

Outputs:

orange

This is because everything between the brackets is treated as a single statement and all variables are expanded before any of the command between the brackets are run. You can work around this using delayed expansion:

setlocal enabledelayedexpansion
set myvar=orange

if 1==1 (
   set myvar=apple
   echo !myvar!
)

Upvotes: 10

Rubens Farias
Rubens Farias

Reputation: 57996

Take a look on IF command help:

C:\Users\Rubens>if /?
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

So, you command must be in same IF line. Your script should be:

@echo off
SET var1="Yes"
SET var2="No"
SET var3="Yes"
if %var1%=="Yes" echo Var1 set
if %var2%=="Yes" echo Var2 set
if %var3%=="Yes" echo Var3 set

Upvotes: 3

paxdiablo
paxdiablo

Reputation: 882716

The echo needs to either be at the end of the if statement:

if %var1%=="Yes" echo Var1 set

or of the following form:

if %var1%=="Yes" (
    echo Var1 set
)

I tend to use the former for very simple conditionals and the latter for multi-command ones and primitive while statements:

:while1
    if %var1%=="Yes" (
        :: Do something that potentially changes var1
        goto :while1
    )

What your particular piece of code is doing is trying to execute the command if %var1%=="Yes" which is not valid in and of itself.

Upvotes: 45

Related Questions