Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Files - Error Handling

Tags:

batch-file

I'm currently writing my first batch file for deploying an asp.net solution. I've been Googling a bit for a general error handling approach and can't find anything really useful.

Basically if any thing goes wrong I want to stop and print out what went wrong.

Can anyone give me any pointers?

like image 735
bplus Avatar asked Jul 22 '09 09:07

bplus


People also ask

How do I check batch file errors?

To test for a specific ERRORLEVEL, use an IF command with the %ERRORLEVEL% variable. n.b. Some errors may return a negative number. If the batch file is being executed as a scheduled task, then exiting with an error code will be logged as a failed task. You can monitor the event log to discover those failures.

What is a batch file error?

Batch files, after executing a command, provides an error code called ERRORLEVEL. A non-zero value means an error has occured. ERRORLEVEL vs %ERRORLEVEL% Both pertain to the current error code, and has the same value.

What does %% mean in batch file?

Required. Represents a replaceable parameter. Use a single percent sign ( % ) to carry out the for command at the command prompt. 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.


2 Answers

I generally find the conditional command concatenation operators much more convenient than ERRORLEVEL.

yourCommand && (   echo yourCommand was successful ) || (   echo yourCommand failed ) 

There is one complication you should be aware of. The error branch will fire if the last command in the success branch raises an error.

yourCommand && (   someCommandThatMayFail ) || (   echo This will fire if yourCommand or someCommandThatMayFail raises an error ) 

The fix is to insert a harmless command that is guaranteed to succeed at the end of the success branch. I like to use (call ), which does nothing except set the ERRORLEVEL to 0. There is a corollary (call) that does nothing except set the ERRORLEVEL to 1.

yourCommand && (   someCommandThatMayFail   (call ) ) || (   echo This can only fire if yourCommand raises an error ) 

See Foolproof way to check for nonzero (error) return code in windows batch file for examples of the intricacies needed when using ERRORLEVEL to detect errors.

like image 197
dbenham Avatar answered Sep 30 '22 13:09

dbenham


Using ERRORLEVEL when it's available is the easiest option. However, if you're calling an external program to perform some task, and it doesn't return proper codes, you can pipe the output to 'find' and check the errorlevel from that.

c:\mypath\myexe.exe | find "ERROR" >nul2>nul if not ERRORLEVEL 1 ( echo. Uh oh, something bad happened exit /b 1 ) 

Or to give more info about what happened

c:\mypath\myexe.exe 2&1> myexe.log find "Invalid File" "myexe.log" >nul2>nul && echo.Invalid File error in Myexe.exe && exit /b 1 find "Error 0x12345678" "myexe.log" >nul2>nul && echo.Myexe.exe was unable to contact server x && exit /b 1 
like image 43
M Jeremy Carter Avatar answered Sep 30 '22 15:09

M Jeremy Carter