5

I have two lines of call command in batch file like this:

call execute.cmd
call launch.cmd

My need is to call the launch.cmd if and only if call to execute.cmd succeeds. So is there any way by which can I put some condition here?

execute.cmd does not return any value here.

Anand
  • 2,239
  • 4
  • 32
  • 48

2 Answers2

5

If execute.cmd returns an integer than you can use a IF command to check it's return value and if it matches the desired one than you can call launch.cmd

Suppose that execute.cmd returns 0 if it is successful or an integer >= 1 otherwise. The batch would look like this:

rem call the execute command
call execute.cmd
rem check the return value (referred here as errorlevel)
if %ERRORLEVEL% ==1 GOTO noexecute
rem call the launch command
call launch.cmd

:noexecute
rem since we got here, launch is no longer going to be executed

note that the rem command is used for comments.

HTH,
JP

Community
  • 1
  • 1
Ioan Paul Pirau
  • 2,733
  • 2
  • 23
  • 26
  • exceute.cmd returns 0 if it is successful and 1 if not, so problem got solved using your logic, thanks a lot. As u replied first, I'm accepting your answer, even though the answer by @jhclark looks correct. – Anand Aug 05 '11 at 13:39
  • @Anand K Gupta your wellcome :) – Ioan Paul Pirau Aug 05 '11 at 14:17
  • You may also achieve the same thing with Windows Batch && combination this way: call execute.cmd && call launch.cmd – Aacini Aug 06 '11 at 04:22
3

I believe this is a duplicate of How do I make a batch file terminate upon encountering an error?.

Your solution here would be:

call execute.cmd
if %errorlevel% neq 0 exit /b %errorlevel%
call launch.cmd
if %errorlevel% neq 0 exit /b %errorlevel%

Unfortunately, it looks like Windows batch files have no equivalent of UNIX bash's set -e and set -o pipefail. If you're willing to abandon the very limited batch file language, you could try Windows PowerShell.

Community
  • 1
  • 1
jhclark
  • 2,493
  • 1
  • 20
  • 14