There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash dns drive driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop mac malware memory monitor motherboard mouse network printer problem ram registry router security server slow software sound trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Solved: Batch file IF statment

Reply  
Thread Tools
srini_vk's Avatar
Junior Member with 6 posts.
 
Join Date: Jun 2009
16-Jun-2009, 06: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, 07: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 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
16-Jun-2009, 08: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 Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

srini_vk's Avatar
Junior Member with 6 posts.
 
Join Date: Jun 2009
17-Jun-2009, 12:27 AM #4
Thank you
Reply

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.

Search Tech Support Guy

Find the solution to your
computer problem!




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



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:53 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.