Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find and extract a substring from a command output?

Before I go into details I'd like to stress the fact that using PowerShell is not an option, because the script is supposed to be run on machine where PowerShell might not be available.

I need to check for a version of Internet Explorer installed on a Windows machine and act upon it depending on which version it is.

So far my batch script looks like this:

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

As you can see I am assigning reg query output to IEVarReg variable. The ouput is different on different Windows versions, examples:

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

Ideally, I'd like to get the first digit from the version number and do something like:

if
  version == 8
then
  execute this
elseif
  version == 9
then
  execute that

I'm sure I'll figure out how to do the if part once I get the version number, but it's the getting version number part I cannot figure out. I could do it easily in bash, perl, etc. but have no idea about Windows Command Line.

Help would be hugely appreciated.

PS. I'v seen Jerold Schulman's script but it returns the whole version number which is too much for me.

like image 322
Michal M Avatar asked Dec 16 '22 04:12

Michal M


2 Answers

you need to put together all of this pieces

  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 (
      ...
    

The first version of the working BAT must be something like this...

@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
like image 183
PA. Avatar answered Mar 04 '23 23:03

PA.


Back in the old days, before I stopped using Microsoft products entirely, I swore by a product called 4DOS from JP Software. The company still exists, and they have kept their product line up to date. It's commercial software, but it made DOS+Windows+OS/2 scripting a breeze. (And yes, REXX is not a breeze.)

4DOS was also licensed by Norton and released as NDOS, which got a bit more press than the original version.

That said, if memory serves, you should also be able to use the SET command to create a variable substring.

SET start=1
SET length=1
SET string=%IEVerReg%
CALL SET substribg=%%string:~%start%,%length%%%

I'd love to know if this works. I don't have a Windows box around to test it on, fortunately.

like image 39
ghoti Avatar answered Mar 04 '23 22:03

ghoti