You could use following at top of your batch file:
@echo off
set "SystemPath=%SystemRoot%\System32"
if not "%ProgramFiles(x86)%" == "" set "SystemPath=%SystemRoot%\Sysnative"
Next you need to call every console application in System32 directory of Windows with %SystemPath%
in your batch file, for example %SystemPath%\findstr.exe
. Of course you could also start cmd with %SystemPath%\cmd.exe
to run always 64-bit command line interpreter from within the batch file.
How it works?
The environment variable SystemPath is set first to System32
directory of Windows.
The batch file packed into a 32-bit executable runs now all console applications indeed from System32
directory on 32-bit Windows, but from %SystemRoot%\SysWOW64
directory on 64-bit Windows.
Therefore the batch file checks next if environment variable ProgramFiles(x86) exists which is the case only on Windows x64. Therefore the condition on third line is false on Windows x86 and SystemPath is not changed. But SystemPath is modified to %SystemRoot%\Sysnative
on 64-bit Windows to call the applications in %SystemRoot%\System32
from 32-bit executable respectively cmd.exe
without redirection to %SystemRoot%\SysWOW64
.
For more details see the Microsoft documentation page File System Redirector.
But better would be to do that all inside the 32-bit executable which extracts the batch file to %TEMP%
and run it either with
%SystemRoot%\System32\cmd.exe /C "%TEMP%\ExtractedBatch.bat"
for 32-bit Windows where environment variable ProgramFiles(x86) does not exist or with
%SystemRoot%\Sysnative\cmd.exe /C "%TEMP%\ExtractedBatch.bat"
on 64-bit Windows.
Here is one more code which can be used at top of a batch file to run always 64-bit console applications independent on being started on Windows x64 with 32-bit or with 64-bit cmd.exe
.
@echo off
set "SystemPath=%SystemRoot%\System32"
if not "%ProgramFiles(x86)%" == "" if exist %SystemRoot%\Sysnative\cmd.exe set "SystemPath=%SystemRoot%\Sysnative"
On Windows x64 it is additionally checked if there are files in %SystemRoot%\Sysnative
. In this case the batch file is executed with 32-bit cmd.exe
and only in this case %SystemRoot%\Sysnative
needs to be used at all. Otherwise %SystemRoot%\System32
can be used also on Windows x64 as when the batch file is started with 64-bit cmd.exe
, this is the directory containing the 64-bit console applications.
Note: %SystemRoot%\Sysnative
is not a directory. It is not possible to cd
to %SystemRoot%\Sysnative
or use if exist %SystemRoot%\Sysnative
.