Can't Capture WMIC DATAFILE output to variable on Windows 7


I am using the following WMIC command to retrieve the version info from a file.  For this example, I am using iexplore.exe

When I execute the command interactively in a command window on Windows XP and Windows 7, I get identical outputs.

Command:
WMIC DATAFILE WHERE "Name='%SYSTEMDRIVE%\\program files\\internet explorer\\iexplore.exe'" GET Version

Output:
Version
8.0.7601.17514


When I try to capture the output thru a "for /f" loop, it works for Windows XP, but I can't get it work for Windows 7.

I have tried variations with "setlocal EnableDelayedExpansion",but with no luck.

Here is the contents of the bat file, IE_version.bat


@echo off
for /f "skip=1 tokens=1 delims= " %%g in ('WMIC DATAFILE WHERE ^"Name^='%SYSTEMDRIVE%\\program files\\internet explorer\\iexplore.exe'^" GET Version') DO (
set IEVER=%%g
)
echo IE Version: %IEVER%


Here is the output on Windows XP:

IE Version: 8.00.6001.18702


Here is the output on Windows 7:

IE Version:


I must be missing something obvious, but cannot find it!

Thanks
MichaelScottPaperApplication SupportAsked:
Who is Participating?
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
Found it already, this version works both for XP and W7, without the need for 'ver':

@echo off
for /f "skip=2 tokens=2 delims== " %%g in ('WMIC DATAFILE WHERE ^"Name^='%SYSTEMDRIVE%\\program files\\internet explorer\\iexplore.exe'^" GET Version /value') DO (
set IEVER=%%g
)
echo IE Version: %IEVER%

Open in new window


XP output

C:\Temp>getiever.cmd
IE Version: 6.00.2900.5512

W7 output

C:\Temp>getiever.cmd
IE Version: 8.0.7601.17514
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Hi Michael,

Here's a working W7 version for you:
@echo off
for /f "skip=2 delims== tokens=2" %%g in ('WMIC DATAFILE WHERE ^"Name^='%SYSTEMDRIVE%\\program files\\internet explorer\\iexplore.exe'^" GET Version /value') DO (
set IEVER=%%g
)
echo IE Version: %IEVER%

Open in new window


Output:

C:\Temp>ver
Microsoft Windows [Version 6.1.7601]

C:\Temp>getiever.cmd
IE Version: 8.0.7601.17514

C:\Temp>
0
 
MichaelScottPaperApplication SupportAuthor Commented:
gerwinjansen,

I confirm it works for Windows 7.  

Unfortunately, for Windows XP, its now not parsing just the version number and is also outputting the extra information "(xpsp.080413-2105)"

I am hoping to have one piece of code to provide the same output without having to have extra code to determine operating system first.


D:\>getiever.cmd
IE Version: 6.00.2900.5512 (xpsp.080413-2105)

D:\>ver

Microsoft Windows XP [Version 5.1.2600]

D:\>



D:\>getiever.cmd
IE Version: 8.0.7601.17514

D:\>ver

Microsoft Windows [Version 6.1.7601]

D:\>
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Gerwin Jansen, EE MVETopic Advisor Commented:
I understand :) Can you show me your output of just the WMIC command on WinXP?
0
 
telczj9Commented:
MichaelScottPaper,

try this, it should work in WinXP and Win7

@echo off
ver | find "XP" > NUL
IF not errorlevel 1 (
  for /f "skip=1 tokens=1 delims= " %%g in ('WMIC DATAFILE WHERE ^"Name^='%SYSTEMDRIVE%\\program files\\internet explorer\\iexplore.exe'^" GET Version') DO (
  set IEVER=%%g
  )
) else (
  for /f "skip=2 delims== tokens=2" %%g in ('WMIC DATAFILE WHERE ^"Name^='%SYSTEMDRIVE%\\program files\\internet explorer\\iexplore.exe'^" GET Version /value') DO (
  set IEVER=%%g
  )
)
echo IE Version: %IEVER% 

Open in new window


This is the output on both win7 and winXP:
C:\>ver

Microsoft Windows [Version 6.1.7601]

C:\>getiever.cmd
IE Version: 8.0.7601.17514

C:\>


C:\>ver

Microsoft Windows XP [Version 5.1.2600]

C:\>getiever.cmd
IE Version: 7.00.6000.17103

Open in new window



Cheers!
0
 
MichaelScottPaperApplication SupportAuthor Commented:
gerwinjansen,

Excellent!

This does exactly what I want with minimal code and I have learned something new!  

Accepting Solution.

Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.