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条回答
ゆ 、 Hurt°
2楼-- · 2019-01-21 08:00

The correct way, as SAM write before is:

reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" /v "Identifier" | find /i "x86" > NUL && set OS=32BIT || set OS=64BIT

but with /v "Identifier" a little bit correct.

查看更多
Evening l夕情丶
3楼-- · 2019-01-21 08:02
set bit=64
IF NOT DEFINED PROGRAMFILES(X86) (
set "PROGRAMFILES(X86)=%PROGRAMFILES%"
set bit=32
)
REM Example 1: REG IMPORT Install%bit%.reg (all compatibility)
REM Example 2: CD %PROGRAMFILES(X86)% (all compatibility)
查看更多
\"骚年 ilove
4楼-- · 2019-01-21 08:03

This is the correct way to perform the check as-per Microsoft's knowledgebase reference ( http://support.microsoft.com/kb/556009 ) that I have re-edited into just a single line of code.

It doesn't rely on any environment variables or folder names and instead checks directly in the registry.

As shown in a full batch file below it sets an environment variable OS equal to either 32BIT or 64BIT that you can use as desired.

@echo OFF

reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /i "x86" > NUL && set OS=32BIT || set OS=64BIT

if %OS%==32BIT echo This is a 32bit operating system
if %OS%==64BIT echo This is a 64bit operating system
查看更多
beautiful°
5楼-- · 2019-01-21 08:05

Run the below in the command prompt:

Start -> Run -> Type cmd and enter the command below in the resulting black box:

wmic os get osarchitecture
查看更多
爷、活的狠高调
6楼-- · 2019-01-21 08:06
*** Start ***

@echo off

Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0

REG.exe Query %RegQry% > checkOS.txt

Find /i "x86" < CheckOS.txt > StringCheck.txt

If %ERRORLEVEL% == 0 (
    Echo "This is 32 Bit Operating system"
) ELSE (
    Echo "This is 64 Bit Operating System"
)

*** End ***

reference http://support.microsoft.com/kb/556009

查看更多
beautiful°
7楼-- · 2019-01-21 08:11

This is a one-liner that will have %errorlevel% of 0 for 64-bit, 1 for non-64-bit. I can't vouch for it working on all versions of Windows, but demonstrates one method for determining it. You can add multiple findstrqueries if you know all the possibilities to look for.

set | findstr /i processo.*64 > nul 2>&1

Basically, you're dumping the environment variables, and using a regular expression to search for something that has "processo" + "64" somewhere in its line. The piping is just to suppress the matching lines. If I changed it to set | findstr /i processo.*64 > nul 2>&1 on my current rig, this would be the result:

 C:\Windows\System32>set | findstr /i processo.*64
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 158 Stepping 9, GenuineIntel

This is a one-liner to see if your processor is a 64-bit AMD

set | findstr /i processo.*amd.*64 > nul 2>&1

You can take these as a starting point and refine them for your requirements. I ended up using this over known environment variable names due to it being more reliable across different major versions of Windows that I was working with.

查看更多
登录 后发表回答