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.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.
[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.
The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With