I have a batch file that needs to be run in a 32-bit context so contains a bit of code to call the 32-bit command processor with its own path. This script also needs to be able to return error code on failure which it does via exit /b 123
. However...
When the exit
is in a (
)
block, AND contains any statement after it, this does not get returned correctly.
@echo off
setlocal EnableDelayedExpansion
rem Ensure we're running in 32-bit mode
if not %PROCESSOR_ARCHITECTURE%==x86 (
echo Thunking to 32-bit mode
%Windir%\SysWOW64\cmd.exe /c %0
echo !ERRORLEVEL!
exit /b !ERRORLEVEL!
)
(
echo before
exit /b 456
echo after
)
The output is as follows:
H:\>sub.bat
Thunking to 32-bit mode
before
0
H:\>
If you remove the echo
after the exit
, then it works exactly as expected.
H:\>sub.bat
Thunking to 32-bit mode
before
456
H:\>
Even if you replace the echo
with a rem
or any other command, it still fails. If you manually run the 32-bit cmd.exe
and run the same script, the exit code gets set correctly.
H:\>sub.bat
before
H:\>echo %ERRORLEVEL%
456
H:\>
Can anyone give an explanation and a workaround for this?