Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Inconsistent return code for TCC scripts

Aug
728
10
An EXTREMELY simple test case. The reproduction restriction is to start a new CMD session and call pre-created batch file.
Code:
Microsoft Windows [Version 6.1.7601]
(c) Корпорация Майкрософт (Microsoft Corp.), 2009. Все права защищены.

$ stc.btm & echo Finish %ERRORLEVEL%
TYPE "%~0"
VER
QUIT 3

TCC LE  14.00.9 x64   Windows 7 [Version 6.1.7601]
Finish 0

$ stc.btm & echo Finish %ERRORLEVEL%
TYPE "%~0"
VER
QUIT 3

TCC LE  14.00.9 x64   Windows 7 [Version 6.1.7601]
Finish 3

$
Code:
Microsoft Windows [Version 6.1.7601]
(c) Корпорация Майкрософт (Microsoft Corp.), 2009. Все права защищены.

$ C:\Programs\TCC-RT\tcc.exe /C stc.btm & echo Finish %ERRORLEVEL%
TYPE "%~0"
VER
QUIT 3

TCC RT  21.00.24 x64   Windows 7 [Version 6.1.7601]
Finish 0

$ C:\Programs\TCC-RT\tcc.exe /C stc.btm & echo Finish %ERRORLEVEL%
TYPE "%~0"
VER
QUIT 3

TCC RT  21.00.24 x64   Windows 7 [Version 6.1.7601]
Finish 3

$
 
I've tested without TCEXIT script. Even if it exists, how could it change the results of the first run and not the next?
 
I think I'm able to explain it. And it is not your fault.
%ERRORLEVEL% being expanded before the batch file is run, thus only on next invocation it receives the actual return code.
The hint is in SETLOCAL help page, see "delayed explansion" section.
Invoking cmd /V:ON and calling the test as
Code:
stc.btm & echo Result !ERRORLEVEL!
solves the issue.
Note to self: never use compound character in CMD ever again.
 

Similar threads

Back
Top