batch file - How do I reduce the codes require to check error -
we check error using
if !errorlevel! neq 0 (do something) but littered everywhere in batch file.
1) way encapsulate log , exit program upon error?
2) how log bat file line number causing error?
@echo off setlocal echo y|find "y" >nul call aberr matching y , y pause echo x|find "y" >nul call aberr matching x , y pause echo y|find "z" >nul call aberr matching y , z pause goto :eof the above testing scenario, setting errorlevel 0, 1, 1 in succession, calling batch aberr.bat analyse result.
here's aberr.bat
@echo off if %errorlevel%==0 goto :eof echo %errorlevel% found %* exit note here there no setlocal (which set errorlevel zero) , routine exits.
consequence if aberr.bat on path message produced show errorlevel found plus text on call aberr line after call aberr.
you place pause after echo %errorlevel% line show result, or log result file using
>>logfile.txt echo %errorlevel% found %*
Comments
Post a Comment