Batch to detect if system is a 32-bit or 64-bit

2020-06-08 14:55发布

Does anybody know how to create a batch file that can shell one program if its a 64-bit system or shell another if its a 32-bit system?

9条回答
Emotional °昔
2楼-- · 2020-06-08 15:45
uname -a #for mac

uname -i #for ubuntu
查看更多
The star\"
3楼-- · 2020-06-08 15:48

The below method should be pretty reliable since it will work even if environment variables have been messed with:

rem If no kernel32.dll in System32, probably running on DOS or 16-bit Windows
if not exist "%SystemRoot%\System32\kernel32.dll" goto DOS

rem If no kernel32.dll in SysWOW64, likely a 32-bit Windows 
if not exist "%SystemRoot%\SysWOW64\kernel32.dll" goto WIN32

rem If file size reported for kernel32.dll located in System32 and SysWOW64 is
rem the same, it likely means that System32 is being redirected to SysWOW64.
rem This would be the case for 32-bit version of cmd.exe running on 64-bit OS. 
for %%I in ("%SystemRoot%\SysWOW64\kernel32.dll") do (
  for %%J in ("%SystemRoot%\System32\kernel32.dll") do (
    if "%%~zI" equ "%%~zJ" goto WOW64
  )
)

rem If we get this far, the script is likely running in native 64-bit console
echo Native shell on 64-bit Windows
rem ...
exit /b

:WOW64
echo 32-bit shell on 64-bit Windows (WOW64)
rem ...
exit /b

:WIN32
echo 32-bit Windows
rem ...
goto END

:DOS
echo DOS or 16-bit Windows
rem ...
goto END

rem ...

:END
rem We can put this label at the end of the file to allow exiting script on 
rem older systems that do not support 'exit /b'

This method relies on the fact that "%WINDIR%\System32\kernel32.dll" should be present on all Windows systems. 64-bit versions of Windows also include "%WINDIR%\SysWOW64" directory containing 32-bit versions of system files, which is not present on 32-bit systems.

On 64-bit systems, 32-bit applications are redirected to SysWOW64 when trying to access files in System32. So, if we get same size kernel32.dll from both System32 and SysWOW64, it means that redirection is in effect and our script is running in 32-bit console on 64-bit OS.

查看更多
贪生不怕死
4楼-- · 2020-06-08 15:51

It works without WMI too! I suggest:

 @echo off
 if /i "%processor_architecture%"=="AMD64" GOTO AMD64
 if /i "%PROCESSOR_ARCHITEW6432%"=="AMD64" GOTO AMD64
 if /i "%processor_architecture%"=="x86" GOTO x86
 GOTO ERR
 :AMD64
    rem do amd64 stuff
 GOTO EXEC
 :x86
    rem do x86 stuff
 GOTO EXEC
 :EXEC
    rem do arch independent stuff
 GOTO END
 :ERR
 @echo Unsupported architecture "%processor_architecture%"!
 pause
 :END
查看更多
登录 后发表回答