If condition in batch files

@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

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!
)

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.


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