batch file to check 64bit or 32bit OS

2019-01-21 07:52发布

Can I check to see if current machine is running 64bit OS or 32bit OS inside a batch file?

EDIT:

Found this online and it is good enough to me now:

22条回答
聊天终结者
2楼-- · 2019-01-21 08:24
set "_os=64"
if "%PROCESSOR_ARCHITECTURE%"=="x86" (if not defined PROCESSOR_ARCHITEW6432 set "_os=32")

Based on: https://blogs.msdn.microsoft.com/david.wang/2006/03/27/howto-detect-process-bitness/

and http://ss64.com/nt/syntax-64bit.html

查看更多
干净又极端
3楼-- · 2019-01-21 08:25

I use either of the following:

:CheckOS
IF EXIST "%PROGRAMFILES(X86)%" (GOTO 64BIT) ELSE (GOTO 32BIT)

:64BIT
echo 64-bit...
GOTO END

:32BIT
echo 32-bit...
GOTO END

:END

or I set the bit variable, which I later use in my script to run the correct setup.

:CheckOS
IF EXIST "%PROGRAMFILES(X86)%" (set bit=x64) ELSE (set bit=x86)

or...

:CheckOS
IF "%PROCESSOR_ARCHITECTURE%"=="x86" (set bit=x86) else (set bit=x64)

Hope this helps.

查看更多
ら.Afraid
4楼-- · 2019-01-21 08:26

Here's my personal favorite, a logical bomb :)

::32/64Bit Switch
ECHO %PROCESSOR_ARCHITECTURE%|FINDSTR AMD64>NUL && SET ARCH=AMD64 || SET ARCH=x86
ECHO %ARCH%
PAUSE

With the AND's (&&) and OR's (||) this is a IF THEN ELSE Batch Construct.

查看更多
▲ chillily
5楼-- · 2019-01-21 08:27

If the script be able to running under Administrator, then it can use the wmi command.

FOR /f "tokens=2 delims==" %%f IN ('wmic os get osarchitecture /value ^| find "="') DO SET "OS_ARCH=%%f"
IF "%OS_ARCH%"=="32-bit" GOTO :32bit
IF "%OS_ARCH%"=="64-bit" GOTO :64bit

ECHO OS Architecture %OS_ARCH% is not supported!
EXIT 1

:32bit
ECHO "32 bit Operating System"
GOTO :SUCCESS

:64bit
ECHO "64 bit Operating System"
GOTO :SUCCESS

:SUCCESS
EXIT 0
查看更多
登录 后发表回答