batch-fileerrorlevel

returning errorlevel from batch function doesnt work


I have some function in batch script:

:run
set "CMD=%*"
<...>
timeout 300s !CMD!
if %errorlevel% equ 0 (
    echo !CMD! >>  ./!_tool!.OK
) else (
    echo !CMD! >>  ./!_tool!.FAIL
    echo exitcode= %errorlevel% >> ./FAIL
    echo ===STOP=== %date% %time%
    exit /b %errorlevel%
)
exit /b %errorlevel%

and im checking its %errorlevel% code in the main cycle:

for /f "tokens=*" %%t in (%TEST_LIST%) do (
    <...>
    call :run %TOOL% -O0 -S %REPO_PATH%\%%t
    if %errorlevel% equ 0 (
        echo %%t PASSED
    ) else (
        echo %%t FAILED
    )

But the issue when timeout 300s !CMD! returns errorlevel 1 and is returning exit /b %errorlevel% as 1 (./!_tool!.FAIL being created and so on) doesnt affect the main cycle's IF and im getting echo %%t PASSED anyway.

Cannot function return code be checked this way or what?

P.S. Some <...> code is working correctly, so I've cut it


Solution

  • Thanks to @Stephan i've found my problem - I should use !errorlevel! instead of %errorlevel% in my FOR loop as it should refresh every iteration