用批处理抓取Windows版本信息

2021-06-10 20:04

阅读:596

标签:time   情况   [1]   batch   his   2012r2   case   简单   min   

[1]Python 环境下比较简单,调用platform 模块即可

[2]没有Python 情况下,可以用ver 命令实现。

 

>ver

Microsoft Windows [Version 10.0.16299.967]

 

自动化用简单的Batch脚本[不推荐]

:: -------------------------------------
:: Check Windows Version
:: 5.0  = W2000
:: 5.1  = XP
:: 5.2  = Server 2003
:: 6.0  = Vista or Server 2008
:: 6.1  = Win7 or Server 2008R2
:: 6.2  = Win8 or Server 2012
:: 6.3  = Win8.1 or Server 2012R2
:: 10.0 = Win10.0,Server 2016 or Server 2019
:: 0.0  = Unknown or Unable to determine
:: --------------------------------------
echo OS Detection:  Starting
ver | findstr /i "5\.0\."
if %ERRORLEVEL% EQU 0 (echo  OS = Windows 2000)
ver | findstr /i "5\.1\."
if %ERRORLEVEL% EQU 0 (echo OS = Windows XP)
ver | findstr /i "5\.2\."
if %ERRORLEVEL% EQU 0 (echo OS = Server 2003)
ver | findstr /i "6\.0\." > nul
if %ERRORLEVEL% EQU 0 (echo OS = Vista / Server 2008)
ver | findstr /i "6\.1\." > nul
if %ERRORLEVEL% EQU 0 (echo OS = Windows 7 / Server 2008R2)
ver | findstr /i "6\.2\." > nul
if %ERRORLEVEL% EQU 0 (echo OS = Windows 8 / Server 2012)
ver | findstr /i "6\.3\." > nul
if %ERRORLEVEL% EQU 0 (echo OS = Windows 8.1 / Server 2012R2)
ver | findstr /i "6\.3\." > nul
if %ERRORLEVEL% EQU 0 (echo OS = Windows 10.0 /Server 2016/Server 2019)

输出结果对,但丑爆,不推荐

技术图片

 

更少代码和更精确的输出

利用FOR 循环.

>for /? 查看帮助

技术图片技术图片
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

    would parse each line in myfile.txt, ignoring lines that begin with
    a semicolon, passing the 2nd and 3rd token from each line to the for
    body, with tokens delimited by commas and/or spaces.  Notice the for
    body statements reference %i to get the 2nd token, %j to get the
    3rd token, and %k to get all remaining tokens after the 3rd.  For
    file names that contain spaces, you need to quote the filenames with
    double quotes.  In order to use double quotes in this manner, you also
    need to use the usebackq option, otherwise the double quotes will be
    interpreted as defining a literal string to parse.

    %i is explicitly declared in the for statement and the %j and %k
    are implicitly declared via the tokens= option.  You can specify up
    to 26 tokens via the tokens= line, provided it does not cause an
    attempt to declare a variable higher than the letter ‘z‘ or ‘Z‘.
    Remember, FOR variables are single-letter, case sensitive, global,
    and you can‘t have more than 52 total active at any one time.

    You can also use the FOR /F parsing logic on an immediate string, by
    making the file-set between the parenthesis a quoted string,
    using single quote characters.  It will be treated as a single line
    of input from a file and parsed.

    Finally, you can use the FOR /F command to parse the output of a
    command.  You do this by making the file-set between the
    parenthesis a back quoted string.  It will be treated as a command
    line, which is passed to a child CMD.EXE and the output is captured
    into memory and parsed as if it was a file.  So the following
    example:

      FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i

    would enumerate the environment variable names in the current
    environment.
for /f "tokens=4-5 delims=. " %%i in (‘ver‘) do set VERSION=%%i.%%j
@echo off
for /f "tokens=4-5 delims=. " %%i in (‘ver‘) do set VERSION=%%i.%%j
if "%version%" == "6.0" echo Windows Vista/Server 2008
if "%version%" == "6.1" echo Windows 7/Server 2008 R2
if "%version%" == "6.2" echo Windows 8/Server 2012
if "%version%" == "6.3" echo Windows 8.1/Server 2012R2
if "%version%" == "10.0" echo Windows 10.0/Server 2016/Server 2019

输出效果:

技术图片

 

[参考]

https://helloacm.com/windows-batch-script-to-detect-windows-version/

 

用批处理抓取Windows版本信息

标签:time   情况   [1]   batch   his   2012r2   case   简单   min   

原文地址:https://www.cnblogs.com/ASAP/p/10593130.html


评论


亲,登录后才可以留言!