Inconsistent return code for TCC scripts

Aug 23, 2010
277
2
#1
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

$
 

rconn

Administrator
Staff member
May 14, 2008
10,621
97
#4
I've tested without TCEXIT script. Even if it exists, how could it change the results of the first run and not the next?
If you have conditional operators (as is common in tcstart and tcexit).

There isn't any code in TCC that could return different results in the first run and the next.

Anyone else able to reproduce this?
 
Aug 23, 2010
277
2
#5
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.