如何从命令输出中找到并提取子字符串?

4

在我详细介绍之前,我想强调一个事实,即使用PowerShell不是一个选择,因为脚本应该在可能没有PowerShell的机器上运行。

我需要检查安装在Windows机器上的Internet Explorer版本,并根据其版本采取行动。

到目前为止,我的批处理脚本看起来像这样:

@echo off
setlocal
for /f "tokens=*" %%a in ( 'reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer" /v Version' ) do ( set IEVerReg=%%a )
endlocal

正如您所看到的,我正在将reg query输出分配给IEVarReg变量。在不同的Windows版本中,输出是不同的,例如:

Windows XP:

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
    Version     REG_SZ  8.0.6001.18702

Windows 7:

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
    Version    REG_SZ    9.0.8112.16421

理想情况下,我希望能够获取版本号的第一个数字,并执行以下操作:
if
  version == 8
then
  execute this
elseif
  version == 9
then
  execute that

我相信一旦我得到版本号,就能够解决if部分的问题,但是我无法弄清楚如何获取版本号。我可以轻松地在bash、perl等中完成它,但对于Windows命令行却毫无头绪。
非常感谢您的帮助。
附注:我看过Jerold Schulman的脚本,但它返回整个版本号,这对我来说太多了。
3个回答

7

您需要将所有这些部分组合在一起。

  1. access the registry to query the version number. See REG QUERY /?

    REG QUERY "HKLM\Software\Microsoft\Internet Explorer" /v Version
    
  2. extract the line that contains the version from the information returned by the REG command. You do this piping the output of REG into the FIND command. See HELP FIND

    REG ... | FIND "REG_SZ" 
    
  3. set the result into a variable. You do it with the FOR /F command. Use tokens=3 to extract the version number that is in the third position of the returned line. Also note the caret ^ that is needed to escape the pipe character |. See HELP FOR

    FOR /F "tokens=3" %%a in ('REG QUERY "HKLM\Software\Microsoft\Internet Explorer" /v Version ^| FIND "REG_SZ"') DO (
      SET v=%%a
    )
    
  4. extract the major version number from the version string. Use delims=. to parse at the period. See HELP FOR. For the appropriate expansion of the variables, you must use the extended expansion and use the ! syntax to access the variables. Also, use /A to treat it as a number. See HELP SET

    FOR /F "tokens=1 delims=." %%m in ('echo !v!') do (
      SET /A m=%%m
    )
    
  5. finally, add your logic for the IE version

    IF .!m!==.8 (
      echo it's 8
    ) ELSE (
      ...
    
工作中的BAT的第一个版本必须类似于这样...
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=3" %%a in ('REG QUERY "HKLM\Software\Microsoft\Internet Explorer" /v Version ^| FIND "REG_SZ"') DO (    
    set v=%%a 
    echo !v!
    FOR /F "tokens=1 delims=." %%m in ('echo !v!') do (
      SET /A m=%%m
    )
    IF !m! EQU 8 (
      echo it's 8
    ) ELSE (
      IF !m! EQU 9 (
        echo it's 9
      ) ELSE (
       echo norrrrr
      )
    )
)
endlocal

3
在我完全停止使用微软产品之前的旧日子里,我信誓旦旦地使用一个叫做4DOS的产品,它来自JP Software。该公司仍然存在,并且他们已经将其产品线更新至今。虽然这是商业软件,但它使得DOS+Windows+OS/2脚本编写变得轻松。(是的,REXX并不容易。)
4DOS还被诺顿授权并发布为NDOS,比原始版本获得了更多的报道。
话虽如此,如果我没记错的话,你也可以使用SET命令创建变量子字符串。
SET start=1
SET length=1
SET string=%IEVerReg%
CALL SET substribg=%%string:~%start%,%length%%%

我想知道这是否有效。幸运的是,我没有Windows电脑可以测试它。


2

这是您提供的代码的轻微修改版本,看起来可以正常工作:

@echo off
setlocal
set qry=reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer" /v Version
set fnd=findstr /I /L /C:"REG_SZ"
for /f "Tokens=2*" %%u in ('%qry%^|%fnd%') do (
 set vsn=%%v
)
@echo %vsn:~0,1%
endlocal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接