# Solved: Batch file IF statment



## srini_vk

Hi

I've to do multiple commands after if. How do I do that.

For e.g., 

IF a == 1 (
echo Its success.
Call abc
..
..
)

Thanks,
Srini


----------



## midders

There are no parentheses in basic batch scripting; instead use labels and goto statements:



Code:


IF string1==string2 goto label1
IF string1==string3 goto label2
goto end

:label1
rem do lots of stuff here
goto end

:label2
rem do lots of other stuff here
goto end

:end
rem end

Don't forget the goto end line after you've done your multiple lines of code otherwise it'll just drop through to the next label and do all that as well.

Sláinte

midders


----------



## TheOutcaste

srini_vk said:


> Hi
> 
> I've to do multiple commands after if. How do I do that.
> 
> For e.g.,
> 
> IF a == 1 (
> echo Its success.
> Call abc
> ..
> ..
> )
> 
> Thanks,
> Srini


You do it just like you have it shown.
*If Condition (*
multiple statements executed if Condition true
*)*

Works with If Else or nested If statements as well
*If Condition (*
multiple statements executed if Condition is true
*)* Else *(*
multiple statements executed if Condition False
* )*


midders said:


> Code:
> 
> 
> IF string1==string2 goto label1
> IF string1==string3 goto label2
> goto end
> 
> :label1
> rem do lots of stuff here
> goto end
> 
> :label2
> rem do lots of other stuff here
> goto end
> 
> :end
> rem end


While it's mostly a matter of preference if you use goto statements and labels instead of parentheses, the above has one potential problem: if string2 and string3 happen to be *equal*, you may want to execute the commands under label2 as well as label1, which the above doesn't allow for.
The following form will catch that particular case. If string2 and string3 aren't equal, it just falls through the 2nd If statement, and the goto statements and labels aren't needed. This only requires 6 lines minimum instead of 9 lines minimum:


Code:


If string1==string2 (
Stuff to do if string1=string2
)
If string1==string3 (
Stuff to do if string1=string3
)

It's also the easiest way to nest If statements


Code:


If [B][COLOR=Blue]Condition1[/COLOR][/B] [COLOR=Red][B]([/B][/COLOR]
  Echo Condition is true, let's check another
  If [COLOR=Blue][B]Condition2[/B][/COLOR] [COLOR=Magenta][B]([/B][/COLOR]
    Echo This is displayed only if Condition1 [I][B]and[/B][/I] Condition2 are both true
  [COLOR=Magenta][B])[/B][/COLOR] Else [COLOR=DarkRed][B]([/B][/COLOR]
    Echo This is displayed only if Condition1 is True [I][B]and[/B][/I] Condition2 is False
  [COLOR=DarkRed][B])[/B][/COLOR]
  Goto _Label1
[COLOR=Red][B] )[/B][/COLOR]
Echo This is executed only if [COLOR=Blue][B]Condition1[/B][/COLOR] is False
Goto :EOF
:_Label1
Echo This is executed only if [COLOR=Blue][B]Condition1[/B][/COLOR] is True

HTH

Jerry


----------



## srini_vk

Thank you


----------

