Question

I have batch file:

@echo off

set vers=0.0
FOR /F "USEBACKQ" %%A IN (`wmic datafile where name^="C:\\Windows\\System32\\msiexec.exe" get Version`) do (set vers=%%A)

echo %vers% 

@pause

Windows XP (SP2):

3.0.3790.2180

Windows Vista (SP1), Windows XP (x64):

ECHO is off.

How to fix that in both situations the batch file executed correctly?

Was it helpful?

Solution

@echo off
    setlocal enableextensions

    set vers=0.0
    FOR /F "tokens=2 delims== usebackq" %%A IN (
        `wmic datafile where name^="C:\\Windows\\System32\\msiexec.exe" get Version /value ^| find "="`
    ) do set "vers=%%A"

    echo %vers% 

    endlocal
pause

It just asks wmic to output data in the format key=value, filters the output of the wmic command searching only lines with = and splits the lines using this character as token delimiter. The first token (the key) is ignored, and second token (the value) is assigned to vers variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top