Batch Files - Error Handling

2020-01-26 07:13发布

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?

标签: batch-file
7条回答
Evening l夕情丶
2楼-- · 2020-01-26 08:09

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
查看更多
登录 后发表回答