批处理文件:检查操作系统是否为 Windows 10
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34317826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch file: Check if OS is Windows 10
提问by Martin Rezyne
I want to make a batch file which will do the following operation: checks if the running OS is windows. If it is than it should print Hello. Im win 10
else should print other message. How can i do this if condition?
我想制作一个批处理文件,它将执行以下操作:检查正在运行的操作系统是否为 windows。如果它比它应该打印Hello. Im win 10
否则应该打印其他消息。如果条件允许,我该怎么做?
Pseudocode:
伪代码:
if OS == Win10 then
echo Hello im win 10
else
echo I am another os
回答by Vertexwahn
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
回答by Stephan
if you want it a bit more detailed:
如果你想要更详细一点:
for /f "tokens=2 delims=," %%i in ('wmic os get caption^,version /format:csv') do set os=%%i
echo Hello, I am %os%
or to just meet your requirements:
或者只是满足您的要求:
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
(the ,version
ensures, your desired string is not the last token, which contains that ugly wmic line ending)
(,version
确保,您想要的字符串不是最后一个标记,其中包含丑陋的 wmic 行结尾)
回答by MarekJ47
I improved Vertexwahn's script to support more Windows versions:
我改进了 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
回答by T3RR0R
Quick and easy...
快捷方便...
wmic OS get OSArchitecture,caption | FIND "10" >nul || GOTO Not_Win10
:: Your Program...
:Not_Win10
ECHO Not Compatible
pause
exit
Without using a label / Goto:
不使用标签/转到:
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
回答by Wasif Hasan
Another easy way:
另一个简单的方法:
@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