?
Solved

conditional execution of commands in a batch file

Posted on 2007-10-01
13
Medium Priority
?
1,468 Views
Last Modified: 2013-12-19
Hi,

I have a question related to .BAT (batch) file processing.

I need to execute several commands (like isql and sqlldr) in a batch.

Does DOS (MS Windows XP) have a possibility to verify if previous command has been executed successfully and only then execute next command?

It should be kind of IF statement in DOS.
0
Comment
Question by:GoodName
  • 7
  • 5
13 Comments
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 400 total points
ID: 19993659
You can do this:

dir dsafasdfsdafasd&&echo hello

The && will only execute the echo statement if the dir command is successful. In this case it will probably not echo "hello" as the file doesn't exist.
0
 
LVL 12

Expert Comment

by:weareit
ID: 19993710
I don't think this is what he was asking for Steve...

using && does not garruntee the results you are most likely looking for...

What you would want to do is either use an application which takes the results from the software and returns something that you can set as a variable to pass to your Batch file.  You can also experiment with ERRORLEVEL in your batch file as most applications will return an ERRORLEVEL to the operating system.

http://www.computerhope.com/jargon/e/erroleve.htm

-saige-
0
 
LVL 12

Expert Comment

by:weareit
ID: 19993724
Also offhand, any particular reason you can't use WSH?

-saige-
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:SteveGTR
ID: 19993731
We'll see...
0
 
LVL 12

Accepted Solution

by:
weareit earned 600 total points
ID: 19994013
Steve to test what you are stating...  If you go to a dos prompt and then run the following:

NET START W32TIME && echo "do some commands" && NET STOP W32TIME && NET START W32TIME

You will clearly see that NET START did in fact run successfully even though the results might not have been what you had hoped...  What if, in this case, the do some commands section could only run if the W32TIME process had just started.  If that were the case then your do some commands section would produce errant results.  By using ERRORLEVEL you could easily avoid the issue.

@ECHO OFF
NET START W32TIME
IF ERRORLEVEL 1 GOTO AStart
IF ERRORLEVEL 0 GOTO NStart
:AStart
ECHO "W32TIME is already started.  Cannot restart."
GOTO End
:NStart
ECHO "W32Time is now started."
:End
PAUSE

More documentation, BTW:  http://www.robvanderwoude.com/errorlevel.html
http://forums.techguy.org/development/620137-errorlevel-batch-not-working.html

-saige-
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19994075
When I run this:

net start w32time&&echo Hello

Hello is not started, because I'm in Vista and I get "access denied".
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19994084
"Hello is not started" should be:

Hello is not displayed

That is exactly how the && works...
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19994137
Check out this for more information that will help you to understand:

http://www.robvanderwoude.com/condexec.html
0
 
LVL 12

Expert Comment

by:weareit
ID: 19994204
Steve I understand what you are saying...  But, && will not always prevent the next command from running as I use the NET STOP "some service" && NET START "some service" all the time...  The reason why && does not work in this case is because the command that is refferenced is the NET command...  Does NET have a sub command called START...  Yes it does, no error...  If on the other hand I specified NET 1 or NET PULL or NET, your method would work...  On vista you are getting an error Access Denied is because of the User Access Security...  But do the same on an XP or earlier system and you will see exactly what I mean...

-saige-
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19994223
Your code is no different (abet more verbose) then mine with the &&.

I ran this on my XP Pro machine:

net start w32time&&echo Hello

It displays:

The requested service has already been started.

More help is available by typing NET HELPMSG 2182

It doesn't print Hello.

Have you tried that?
0
 

Author Comment

by:GoodName
ID: 19994297
Thank you guys for your timely help!
0
 
LVL 12

Expert Comment

by:weareit
ID: 19994333
Your welcome GoodName...
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19996048
weareit any comment? Perhaps I'm missing something, if so I'd like to know so that I can understand what you are saying. Thanks :)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question