Batch Files - Error Handling

后端 未结 7 1677
谎友^
谎友^ 2020-11-28 08:54

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

相关标签:
7条回答
  • 2020-11-28 09:53

    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
    
    0 讨论(0)
提交回复
热议问题