批处理文件:检查操作系统是否为Windows 10

问题描述 投票:0回答:7

我想制作一个批处理文件,它将执行以下操作:检查正在运行的操作系统是否是Windows。如果是,则应打印

Hello. Im win 10
,否则应打印其他消息。如果有条件我该怎么做?

伪代码:

if OS == Win10 then
   echo Hello im win 10
else
   echo I am another os
windows batch-file cmd operating-system detect
7个回答
21
投票
setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" == "6.3" echo Windows 8.1
if "%version%" == "6.2" echo Windows 8.
if "%version%" == "6.1" echo Windows 7.
if "%version%" == "6.0" echo Windows Vista.
if "%version%" == "10.0" echo Windows 10.
echo %version%
rem etc etc
endlocal

6
投票

如果您想要更详细一点:

for /f "tokens=2 delims=," %%i in ('wmic os get caption^,version /format:csv') do set os=%%i
echo Hello, I am %os%

或者只是满足您的要求:

for /f "tokens=2 delims=," %%i in ('wmic os get caption^,version /format:csv') do set os=%%i
echo %os%|find " 10 ">nul &&echo Hello I'm Windows 10||echo I am another os

,version
确保您想要的字符串不是最后一个标记,其中包含丑陋的 wmic 行结尾)


3
投票

我改进了Vertexwahn的脚本以支持更多Windows版本:

setlocal
for /f "tokens=2 delims=[]" %%i in ('ver') do set VERSION=%%i
for /f "tokens=2-3 delims=. " %%i in ("%VERSION%") do set VERSION=%%i.%%j
if "%VERSION%" == "5.00" echo Windows 2000
if "%VERSION%" == "5.0" echo Windows 2000
if "%VERSION%" == "5.1" echo Windows XP
if "%VERSION%" == "5.2" echo Windows Server 2003
if "%VERSION%" == "6.0" echo Windows Vista
if "%VERSION%" == "6.1" echo Windows 7
if "%VERSION%" == "6.2" echo Windows 8
if "%VERSION%" == "6.3" echo Windows 8.1
if "%VERSION%" == "6.4" echo Windows 10
if "%VERSION%" == "10.0" echo Windows 10
echo %VERSION%
endlocal

0
投票

另一个简单的方法:

@Echo Off
SystemInfo | Find "OS Name:" >tmp.txt
Type tmp.txt | Find "10" >nul 2>&1  || (
  Del tmp.txt
  Echo OS Not Windows 10
  Pause 
  Exit /B
)

Del tmp.txt

:: Main Program

0
投票

快速又简单...

wmic OS get OSArchitecture,caption | FIND "10" >nul || GOTO Not_Win10

:: Your Program...

:Not_Win10
ECHO Not Compatible
pause
exit

不使用标签/转到:

wmic OS get OSArchitecture,caption | FIND "10" >nul || ECHO System not Windows 10 && TIMEOUT 3 >nul && EXIT

ECHO System is Windows 10.
pause
::: Your Program

0
投票

我知道这是一篇旧帖子,但我想通过上述版本来发布。使用系统信息对我来说太慢了;我想要更多的服务器版本检测

@echo off

setlocal

REM had to wrap this to catch errors on server 2008
(for /f "tokens=2 delims=," %%i in ('wmic os get caption^,version /format:csv') do set tmp=%%i)>nul 2>&1

if "%tmp%" equ "" (
    set os=Error During OS lookup
) else (
    set os=%tmp%
)

echo %os%


    echo %os% | findstr /C:"Server" 1>nul
    if errorlevel 1 (       
        net statistics server>nul 2>&1
        if errorlevel 1 (
            set isServer=0
        ) else (
            # rem for server 2008, but also final check
            set isServer=1
        )
    ) else (
        set isServer=1
    )



for /f "tokens=2 delims=[]" %%i in ('ver') do set VERSION=%%i
for /f "tokens=2-3 delims=. " %%i in ("%VERSION%") do set VERSION=%%i.%%j
if "%VERSION%" == "5.00" echo Windows 2000
if "%VERSION%" == "5.0" echo Windows 2000
if "%VERSION%" == "5.1" echo Windows XP
if "%VERSION%" == "5.2" echo Windows Server 2003
if "%VERSION%" == "6.0" (
    if "%isServer%" equ "0" (
        echo Windows Vista
    ) else (
        echo Server 2008 Enterprise
    )

)
if "%VERSION%" == "6.1" (
    if "%isServer%" equ "0" (
        echo Windows 7
    ) else (
        echo Server 2008
    )

)
if "%VERSION%" == "6.2" echo Windows 8
if "%VERSION%" == "6.3" (
    if "%isServer%" equ "0" (
        echo Windows 8.1
    ) else (
        echo Server 2012
    )
)
if "%VERSION%" == "6.4" echo Windows 10
if "%VERSION%" == "10.0" (
    if "%isServer%" equ "0" (
        echo Windows 10
    ) else (
        echo %os% | findstr /C:"2016" 1>nul
        if not errorlevel 1 (
            echo Server 2016
        )
        echo %os% | findstr /C:"2019" 1>nul
        if not errorlevel 1 (
            echo Server 2019
        )
    )
)

echo %VERSION%
echo %isServer%
endlocal

pause

0
投票

根据我面前的所有精彩答案,这个答案也可以检测 Windows 11。

@echo off
setlocal
for /f "tokens=2 delims=[]" %%i in ('ver') do set FULLVERSION=%%i
for /f "tokens=2-5 delims=. " %%i in ("%FULLVERSION%") do set         FULLVERSION=%%i.%%j.%%k.%%l
echo Full version: %FULLVERSION%

for /f "tokens=1-2 delims=. " %%i in ("%FULLVERSION%") do set VERSION=%%i.%%j
echo Version: %VERSION%

for /f "tokens=3-3 delims=. " %%i in ("%FULLVERSION%") do set BUILDMAJOR=%%i
echo Build major: %BUILDMAJOR%

set VERSIONNAME=Unknown
if "%VERSION%" == "5.00" set VERSIONNAME=2000
if "%VERSION%" == "5.0" set VERSIONNAME=2000
if "%VERSION%" == "5.1" set VERSIONNAME=XP
if "%VERSION%" == "5.2" set VERSIONNAME=Server 2003
if "%VERSION%" == "6.0" set VERSIONNAME=Vista
if "%VERSION%" == "6.1" set VERSIONNAME=7
if "%VERSION%" == "6.2" set VERSIONNAME=8
if "%VERSION%" == "6.3" set VERSIONNAME=8.1
if "%VERSION%" == "6.4" set VERSIONNAME=10
if "%VERSION%" == "10.0" (
    if %BUILDMAJOR% GEQ 22000 set VERSIONNAME=11
    set VERSIONNAME=10
)
echo Windows %VERSIONNAME%
endlocal
© www.soinside.com 2019 - 2024. All rights reserved.