windows - Is there an easy way to capture the error code (%errorlevel%) from a nested batch script? -
i have nested batch script , want error codes percolate main batch script called it. have tried
exit /b %errorlevel%
but variable doesn't make back. echo'ing %errorlevel% in called batch script gives me 103, echo'ing %errorlevel% in main batch script (the next line in terms of execution) gives me 0. question has been asked on before, none of posts have worked me.
edit: due poor writing, have revised question , add code see.
here main batch file. if statement here never hit unless change condition other 0:
call buildinstaller.cmd %sourcedir% %targetdir% %productversion% %%i if %errorlevel% neq 0 ( echo %date% %time%: build of %%i failed, halting >> %logfile% exit /b %errorlevel% )
here buildinstaller.cmd exits. have cleaned prints avoid confusion:
if %errorlevel% neq 0 ( exit /b %errorlevel% )
as side note, tried doing
set returnvalue=12
in called batch script, echo'ing %returnvalue% in main batch script 1 execution of program behind. if knows answer sub-question, cool know.
you have classical delayed expansion error. see example:
call buildinstaller.cmd %sourcedir% %targetdir% %productversion% %%i if %errorlevel% neq 0 ( echo %date% %time%: build of %%i failed, halting >> %logfile% exit /b %errorlevel% )
this buildinstaller.cmd:
@echo off setlocal enabledelayedexpansion if 1 == 1 ( rem inside code block, %errorlevel% have same value had before block rem (its %value% not updated inside block) need use !errorlevel! rem in order *updated* value verify set errorlevel = 1 if !errorlevel! neq 0 ( exit /b !errorlevel! ) ) exit /b
for further details, "delayed expansion".
Comments
Post a Comment