有人知道如何创建一个批处理文件,如果是64位系统就运行一个程序,如果是32位系统就运行另一个程序吗?
有人知道如何创建一个批处理文件,如果是64位系统就运行一个程序,如果是32位系统就运行另一个程序吗?
检查 %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
)
cmd
实例来工作。 - Joeycmd
实例可能已经被32位进程启动,用户可能只提供命令,无法控制启动命令提示符的内容。(哦,顺便说一下:如果由于任何原因禁用了WMI,则该解决方案将无法工作。) - user541686它不需要 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
if /i "%processor_architecture%"=="x86" ( rem 32 ) else ( rem 64 )
- mcontuname -a #for mac
uname -i #for ubuntu
一种简单的方法是测试 %SystemRoot%\SysWOW64
文件夹是否存在。虽然它不是100%可靠,但非常适合检测系统是否为64位。
所有批处理文件在我的Windows 8 x64上都无法工作。
以下内容与我兼容:
@cd %programfiles(x86)%\
@if %ERRORLEVEL% == 0 (echo x64&&pause)
@if %ERRORLEVEL% == 1 (echo x86&&pause)
这行代码可以给你想要的东西,在XP、Vista和7上都可以使用
将其保存为.bat或.cmd文件
If Defined ProgramFiles(x86) (\\Fileserver\Distribution\Softwarex64.exe) else (\\Fileserver\Distribution\Softwarex86.exe)
如果本地机器的安装源只是指向它(D:\Programs\Softwarex64.exe),
如果您只想运行命令而不是安装,请在第一个括号之间键入您想要的x64命令,在第二个括号之间键入x86命令。
If Defined ProgramFiles(x86) (ipconfig /all & @echo This Is A 64-bit System ) else (arp -a & @echo This Is A 32-bit System)
将此复制到您的CMD中进行测试
希望这可以帮助到您
ubuntu# arch
x86_64
以下方法应该非常可靠,即使环境变量被搞乱了也能正常工作:
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'