Sidharth
Sidharth

Reputation: 1462

How to make batch file stop when command fails

How do i make a batch file to stop running other/next commands if first or any other command fails.

call grails clean 
call grails compile 
call grails package
call grails war 

If grails clean fail then grails compile should not run and same with others.

Upvotes: 3

Views: 7794

Answers (1)

Sourav Gupta
Sourav Gupta

Reputation: 41

The ERRORLEVEL environment variable holds the return value of the last executed command. So you can use:

IF %ERRORLEVEL% NEQ 0 (
  echo "Previous command execution failed."
  exit %ERRORLEVEL%
)

Please see http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html for details.

Upvotes: 4

Related Questions