Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
DOS/PDA/Other
Tag Cloud
access audio black screen blue screen boot bsod connection crash dell desktop driver dvd email error excel excel 2003 firefox hard drive hardware hijackthis internet keyboard laptop malware monitor network networking outlook problem processor ram recovery router safe mode screen slow sound spyware tdlwsp.dll trojan vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
Solved: Batch file IF statment

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
srini_vk's Avatar
Junior Member with 6 posts.
 
Join Date: Jun 2009
16-Jun-2009, 05:56 PM #1
Solved: Batch file IF statment
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's Avatar
Account Closed with 654 posts.
 
Join Date: Dec 1969
16-Jun-2009, 06:12 PM #2
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's Avatar
Computer Specs
Distinguished Member with 5,486 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
16-Jun-2009, 07:05 PM #3
Quote:
Originally Posted by srini_vk View Post
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
)
Quote:
Originally Posted by midders View Post
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 Condition1 (
  Echo Condition is true, let's check another
  If Condition2 (
    Echo This is displayed only if Condition1 and Condition2 are both true
  ) Else (
    Echo This is displayed only if Condition1 is True and Condition2 is False
  )
  Goto _Label1
 )
Echo This is executed only if Condition1 is False
Goto :EOF
:_Label1
Echo This is executed only if Condition1 is True
HTH

Jerry
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers ; I just don't always match the answers to the right questions
Are you aware of the New Signature Limitations?
srini_vk's Avatar
Junior Member with 6 posts.
 
Join Date: Jun 2009
16-Jun-2009, 11:27 PM #4
Thank you
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 12:42 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.