3

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.

4
  • 2
    Is grails a batch file or an executable? Please specify in your batch file grails with file extension. The command call is needed only for batch files, not for console applications. To exit a batch file on previous command exited not successful use if errorlevel 1 goto :EOF or if errorlevel 1 exit /B 1. Run in a command prompt window if /? and goto /? for help on those two commands. Run in command prompt window also exit /? for help on this command in case of grails is a batch file which needs to exit with an exit code greater 0 on error for parent batch file exit. Commented Oct 20, 2016 at 6:34
  • 1
    Possible duplicate of How do I make a batch file terminate upon encountering an error? Commented Oct 20, 2016 at 6:37
  • grails is a batch file @Mofi Commented Oct 20, 2016 at 6:49
  • 1
    If grails sets the ErrorLevel, an if not ErrorLevel 1 (meaning ErrorLevel < 1) query could be used; if grails sets the exit code, use && operator (execute next command in case of zero exit code); ErrorLevel and exit code are equal most of the time but are actually different things... Commented Oct 20, 2016 at 9:23

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't %ERRORLEVEL% inside the if be the result of echo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.