Batch Files - Error Handling

I am currently writing my first batch file to deploy an asp.net solution. I searched a little Google for a general approach to error handling and did not find anything useful.

Basically, if something goes wrong, I want to stop and print what went wrong.

Can anyone give me any directions?

+49
batch-file
Jul 22 '09 at 9:15
source share
7 answers

I usually find that conditional statement concatenation operators are 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 causes an error.

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

The fix is ​​to insert a secure team that is guaranteed to be successful at the end of the success branch. I like to use (call ) , which does nothing, except that ERRORLEVEL is 0. There is a consequence (call) , which does nothing, except setting ERRORLEVEL to 1.

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

See Incorrect way to check for an unnecessary (error) return code in a Windows batch file for examples of the subtleties required when using ERRORLEVEL to detect errors.

+51
Jun 13 '13 at 11:27
source share

Besides ERRORLEVEL, batch files do not have error handling. You want to look at a more powerful scripting language. I translated the code in PowerShell .

Being able to easily use .Net assemblies and methods was one of the main reasons I started with PowerShell. Improved error handling was different. The fact that Microsoft now requires that all server programs (Exchange, SQL Server, etc.) be managed by PowerShell are pure icing on the cake.

Right now, it looks like any time spent learning and using PowerShell will be spent enough time.

+7
Jul 22 '09 at 10:52
source share

Using ERRORLEVEL when available is the easiest option. However, if you call an external program to perform some task, and it does not return the correct codes, you can pass the output to "find" and check this error level.

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

Or to find out more 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 
+4
Aug 27 '13 at 16:55
source share

Successful ping on the local network can be captured using ERRORLEVEL .

 @ECHO OFF PING 10.0.0.123 IF ERRORLEVEL 1 GOTO NOT-THERE ECHO IP ADDRESS EXISTS PAUSE EXIT :NOT-THERE ECHO IP ADDRESS NOT NOT EXIST PAUSE EXIT 
+3
Jul 09 '13 at 13:38
source share

I assume that this function was added since OP, but for future source errors that will be displayed in the command window, you can redirect to a file that does not depend on standard output

command 1> file - write the standard output of the command to a file

command 2> file - write a standard command error to a file

0
Feb 06 '15 at 15:36
source share

Python Unittest, Bat process error codes:

 if __name__ == "__main__": test_suite = unittest.TestSuite() test_suite.addTest(RunTestCases("test_aggregationCount_001")) runner = unittest.TextTestRunner() result = runner.run(test_suite) # result = unittest.TextTestRunner().run(test_suite) if result.wasSuccessful(): print("############### Test Successful! ###############") sys.exit(1) else: print("############### Test Failed! ###############") sys.exit() 

Bat Codes:

 @echo off for /l %%a in (1,1,2) do ( testcase_test.py && ( echo Error found. Waiting here... pause ) || ( echo This time of test is ok. ) ) 
0
Sep 24 '17 at 7:54 on
source share

It is very easy! Create a file containing:

 call <filename> // the file you made cls echo An error occured! <Your commands> pause 

So, now that you run it, it will launch your program as usual. But when something goes wrong, it exits and continues the script inside the first file. Now you can enter your own commands.

-four
Nov 17 '16 at 19:51
source share



All Articles