You can use the following at the top of your batch file:
@echo off set "SystemPath=%SystemRoot%\System32" if not "%ProgramFiles(x86)%"=="" set "SystemPath=%SystemRoot%\Sysnative"
Then you need to call any console application in the System32 Windows directory with %SystemPath% in the batch file, for example %SystemPath%\findstr.exe . Of course, you can also run cmd with %SystemPath%\cmd.exe to always launch the 64-bit command line interpreter from a batch file.
How it works?
The SystemPath environment variable is first set to the Windows System32 directory.
A batch file packaged in a 32-bit executable now starts all console applications from the System32 directory on 32-bit Windows, but from the %SystemRoot%\SysWOW64 on 64-bit Windows.
Therefore, the batch file checks the following if the ProgramFiles (x86) environment variable exists, which occurs only for Windows x64. Therefore, the condition on the third line is incorrect for Windows x86, and SystemPath has not been changed. But SystemPath changed to %SystemRoot%\Sysnative on 64-bit Windows to call applications from %SystemRoot%\System32 from the 32-bit executable file respectively cmd.exe without redirecting to %SystemRoot%\SysWOW64 .
For more information, see the File Redirector on the Microsoft Developer Network (MSDN).
But it would be better to do all this inside a 32-bit executable file, which extracts the batch file in %TEMP% and launches it either with
%SystemRoot%\System32\cmd.exe /C "%TEMP%\ExtractedBatch.bat"
for 32-bit Windows where the ProgramFiles (x86) environment variable does not exist or with
%SystemRoot%\Sysnative\cmd.exe /C "%TEMP%\ExtractedBatch.bat"
on 64-bit Windows.
Here is another code that you can use at the top of the batch file to always launch 64-bit console applications, whether it runs on Windows x64 with 32-bit or 64-bit cmd.exe .
@echo off set "SystemPath=%SystemRoot%\System32" if not "%ProgramFiles(x86)%"=="" ( if exist %SystemRoot%\Sysnative\* set "SystemPath=%SystemRoot%\Sysnative" )
Windows x64 additionally checks for the presence of files in %SystemRoot%\Sysnative . In this case, the batch file is executed with 32-bit cmd.exe and only in this case %SystemRoot%\Sysnative should be used at all. Otherwise, %SystemRoot%\System32 can also be used on Windows x64, as when running a batch file with 64-bit cmd.exe , it is a directory containing 64-bit console applications.
Note: %SystemRoot%\Sysnative not a directory. Cannot cd to %SystemRoot%\Sysnative or use if exist %SystemRoot%\Sysnative