# Multiple commands for a batch if statement



## gsingh2011

Hey, this is probably a noob question. I need to make an if statement do two commands but I don't know how. For example:


Code:


if %var%==1 set a=1 [COLOR=Red]and[/COLOR] set b=2

So basically how should I replace the "and".


----------



## TheOutcaste

Use the & for combining two commands. With Set you also need to use parenthesis, so Set knows where it's data ends:


Code:


if %var%==1 (set a=1) [COLOR=Red]&[/COLOR] set b=2

If var=1, set a=1 then set b=2
&& does an *If and only if 1st command succeeds*


Code:


if %var%==1 (set a=1) [COLOR=Red]&&[/COLOR] set b=2

If var=1, set a=1 and then _*if and only if set a=1 was successful*_, set b=2
You can also do an *If and only if 1st command fails* by using *||*



Code:


copy c:\file1.txt f:\backup\file1.txt || (echo Copy failed & goto error)

The *Copy failed* message, and the *goto error* will only execute if the copy command returns an error.

HTH

Jerry


----------

