4

I have created a couple of batch files to aid in the automation process of my deployments. They work great, but I want to be able to have the file bomb out if a problem is found, obviously reporting the problem.

I am planning on integrating the scripts into a TeamCity build, so if one of the scripts fails I need to stop the deployment process.

One easy way is to make the script running a manual process, but that defeats the objective.

Any advice would be great.

Thanks,

JamesStuddart
  • 2,581
  • 3
  • 27
  • 45
  • what's wrong with `exit` command? – npocmaka Feb 10 '16 at 09:30
  • 1
    This just jumps out the batch file, I need to report an error msg, and if possible a code. – JamesStuddart Feb 10 '16 at 09:55
  • I just went and looked it up, and you can pass in an int value for the exit code. If you want to write that as the answer @npocmaka I'll mark it as the answer. Thanks, – JamesStuddart Feb 10 '16 at 09:57
  • But `exit /b ` doesn't stop the batch file anymore, it only returns from one call level. It exits the batch file only in the case when it's already at the top level – jeb Feb 10 '16 at 11:00
  • Try a look at [SO: Exit batch script from inside a function:](http://stackoverflow.com/q/3227796/463115) and for batch exception handling at [DosTips: Exception Handling...](http://www.dostips.com/forum/viewtopic.php?f=3&t=6497) This stuff is a bit advanced – jeb Feb 10 '16 at 11:09
  • try this answer http://stackoverflow.com/a/18471376 – karthick23 Feb 10 '16 at 11:32

1 Answers1

1

Try this

 @code
        IF %ERRORLEVEL% NEQ 0 GOTO ProcessError    
        @code
        exit /b 0    
        :ProcessError
        @codeprocess error
        exit /b 1
karthick23
  • 1,313
  • 1
  • 8
  • 15