I have a control_script.cmd that works as it is but is a little hard to read. Basically, I want to quit my script when the first program returns an error.
set text=converting something...
call %libdir%\1_convert_xlsx.cmd
set rvalue=%errorlevel%
call :check_errorlevel !rvalue! "!text!"
if !rvalue! geq 1 goto :my_error_handler
set text=loading something...
call %libdir%\2_load_something.cmd
set rvalue=%errorlevel%
call :check_errorlevel !rvalue! "!text!"
if !rvalue! geq 1 goto :my_error_handler
:check_errorlevel
set rvalue=%~1
set text=%~2
echo do something with rvalue and text
exit /b 0
:my_error_handler
exit /b 1
it would be much nicer if I could move the if-clause into the check_errorlevel part, like
call %libdir%\1_convert_xlsx.cmd
call :check_errorlevel %errorlevel% "converting something..."
call %libdir%\2_load_something.cmd
call :check_errorlevel %errorlevel% "loading something..."
:check_errorlevel
set rvalue=%~1
set text=%~2
echo do something with rvalue and text
if !rvalue! geq 1 goto :my_error_handler
exit /b 0
:my_error_handler
exit /b 1
But here I face the issue that :my_error_handler is not defined within :check_errorlevel and I do not see any other way to go directly from :check_errorlevel to the end of my cmd. Is there any?
Best regards, Peter
%So after @aschipfl hint, I am going to go for the code below. I actually still need to test it but I am having a good feeling here.
call %libdir%\1_convert_xlsx.cmd
call :check_errorlevel %errorlevel% "converting something..." || goto :my_error_handler
call %libdir%\2_load_something.cmd
call :check_errorlevel %errorlevel% "loading something..." || goto :my_error_handler
goto :all_fine
:check_errorlevel
set rvalue=%~1
set text=%~2
echo do something with rvalue and text
if %rvalue% GEQ 1 ( set my_errorlevel = 1 ) else ( set my_errorlevel = 0 )
REM notice that this errorlevel only transfers the input-errorlevel and is not an errorlevel by itself.
exit /b %my_errorlevel%
:my_error_handler
exit /b 1
:all_fine
exit /b 0
edit: {} to () thanks to stefano