用批处理抓取Windows版本信息

Posted asap

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用批处理抓取Windows版本信息相关的知识,希望对你有一定的参考价值。

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

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

 

>ver

Microsoft Windows [Version 10.0.16299.967]

 

<a>自动化用简单的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)

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

技术图片

 

<b>更少代码和更精确的输出

利用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版本信息的主要内容,如果未能解决你的问题,请参考以下文章

如何用Java抓取网页的具体内容

python怎么抓取网页中DIV的文字

Oracle 数据库 - 使用UEStudio修改dmp文件版本号,解决imp命令恢复的数据库与dmp本地文件版本号不匹配导致的导入失败问题,“ORACLE error 12547”问题处理(代码片段

1-1 用Python抓取豆瓣及IMDB上的电影信息

用正则表达式抓取豆瓣数据

如何管理在每个 git 版本中添加私有代码片段?