Get output from WMIC via a for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Get output from WMIC via a for loop

#1 Post by Matt Williamson » 09 Mar 2015 11:26

Normally when I need to get the output of a WMIC query without the extra LF, I just pass it through a second For sub with %* and everything works as expected. This time though, it isn't working. I'm trying to list the available, working NICs and get the IPaddress, subnetmask and gateway for them. This is what I've tried:

Code: Select all

::@echo off
setlocal

Call :wmic nicconfig where "IPConnectionMetric!=''" get ipaddress,macaddress,defaultipgateway,caption /format:list
exit /b

:wmic
for /f "delims=" %%A in ('"wmic %*"') do for /f "delims=" %%B in ("%%A") do echo %%B
exit /b


And

Code: Select all

::@echo off
setlocal

Call :wmic nicconfig where "IPConnectionMetric>0" get ipaddress,macaddress,defaultipgateway,caption /format:list
exit /b

:wmic
for /f "delims=" %%A in ('"wmic %*"') do for /f "delims=" %%B in ("%%A") do echo %%B
exit /b


But neither work and I'm not sure why. Any clues? I'm hoping it's something simple.

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Get output from WMIC via a for loop

#2 Post by Matt Williamson » 09 Mar 2015 12:34

Rojo helped me out on SO. I just needed to double up on the escaping of the commas. Simple as that!

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Get output from WMIC via a for loop

#3 Post by Compo » 09 Mar 2015 12:47

…like thus:

Code: Select all

@Echo Off
SetLocal

Call :wmic NICConfig Where "IPConnectionMetric Is Not Null" Get Caption^^,DefaultIPGateway^^,IPAddress^^,MACAddress /Value
Exit/B

:wmic
For /F "Tokens=*" %%A In ('WMIc %*') Do For /F "Tokens=*" %%B In ("%%A") Do Echo(%%B
Exit/B

Post Reply