1

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

mechanic
  • 128
  • 1
  • 10
  • 2
    may it be, you use [delayed expansion](http://stackoverflow.com/a/30284028/2152082) at the wrong places? – Stephan May 18 '16 at 16:38
  • @Stephan i use it from the start of the script (dont really know when i cannot use it, only know cycles require it), how can it mess things up related to error codes? – mechanic May 18 '16 at 16:40
  • 1
    well - you use `%errorlevel%` within a block where you should use `!errorlevel!` - and I see no reason to use `!cmd!` instead of `%cmd%` (I don't say, there _ is_ no reason, but from what I can see, there's none) – Stephan May 18 '16 at 16:51
  • @Stephan what block are you about? FOR cycle or the `:run` function? Should I use ALL the vars in any block () like !this! ? – mechanic May 18 '16 at 21:57
  • 1
    I'm about _all_ blocks. Only vars that changed in that same block have to be delayed. See [my examples](http://stackoverflow.com/a/30284028/2152082) and the linked questions there. Also [ss64](http://ss64.com/nt/delayedexpansion.html) is a good starting point. – Stephan May 19 '16 at 05:39
  • @Stephan thank you, I should try using delayed errorlevel and report back soon – mechanic May 19 '16 at 07:00

1 Answers1

0

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

mechanic
  • 128
  • 1
  • 10