批量检测系统是32位还是64位

时间:2011-02-14 10:03:01

标签: shell batch-file 32bit-64bit

有人知道如何创建一个批处理文件,如果它是一个64位系统,那么它可以为一个程序提供shell,如果它是一个32位系统,那么它是另一个程序吗?

9 个答案:

答案 0 :(得分:23)

检查%PROCESSOR_ARCHITECTURE%是否为x86

if %PROCESSOR_ARCHITECTURE%==x86 (
  rem 32 bit
) else (
  rem 64 bit
)

至少目前是这样。例如,在服务器上,我可以访问它AMD64但不知道Itanium的样子。但32位版本始终报告x86

另一种选择,也适用于WoW64:

for /f "skip=1 delims=" %%x in ('wmic cpu get addresswidth') do if not defined AddressWidth set AddressWidth=%%x

if %AddressWidth%==64 (
  rem 64 bit
) else (
  rem 32 bit
)

答案 1 :(得分:12)

没有WMI也可以使用! 我建议:

 @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

答案 2 :(得分:5)

uname -a #for mac

uname -i #for ubuntu

答案 3 :(得分:3)

简单的方法是测试%SystemRoot%\SysWOW64文件夹的存在。虽然它不是100%万无一失,但它对于检测系统是否为64位非常有用。

答案 4 :(得分:2)

所有批处理都无法在我的Windows 8 x64中运行。

跟我一起工作:

@cd %programfiles(x86)%\

@if %ERRORLEVEL% == 0 (echo x64&&pause)

@if %ERRORLEVEL% == 1 (echo x86&&pause)

答案 5 :(得分:1)

这条线将为您提供您想要的,适用于XP,Vista和7

将其另存为.bat或.cmd

If Defined ProgramFiles(x86) (\\Fileserver\Distribution\Softwarex64.exe) else (\\Fileserver\Distribution\Softwarex86.exe)

如果本地计算机中的安装源只指向它(D:\ Programs \ Softwarex64.exe)

如果你只想运行命令而不是安装,只需在第一个()和x86命令之间输入你想要的x64命令在第二个()之间

If Defined ProgramFiles(x86) (ipconfig /all & @echo  This Is A 64-bit System ) else (arp -a & @echo This Is A 32-bit System)

将其复制到您的CMD中进行测试

我希望这会有所帮助

答案 6 :(得分:0)

答案 7 :(得分:0)

在Linux上,您只需在命令行中使用“arch”。

ubuntu# arch
x86_64

在OSX(Snow Leopard)上,即使您使用的是64位硬件,也会返回“i386”。

答案 8 :(得分:0)

以下方法应该非常可靠,因为即使环境变量已被搞乱,它也能正常工作:

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'

此方法依赖于“%WINDIR%\ System32 \ kernel32.dll”应存在于所有Windows系统上的事实。 64位版本的Windows还包含“%WINDIR%\ SysWOW64”目录,其中包含32位版本的系统文件,这在32位系统上不存在。

在64位系统上,当尝试访问System32中的文件时,32位应用程序被重定向到SysWOW64。因此,如果我们从System32和SysWOW64获得相同大小的kernel32.dll,则意味着重定向生效,我们的脚本在64位操作系统上的32位控制台中运行。