Page 1 of 1

Get output from WMIC via a for loop

Posted: 09 Mar 2015 11:26
by Matt Williamson
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.

Re: Get output from WMIC via a for loop

Posted: 09 Mar 2015 12:34
by Matt Williamson
Rojo helped me out on SO. I just needed to double up on the escaping of the commas. Simple as that!

Re: Get output from WMIC via a for loop

Posted: 09 Mar 2015 12:47
by Compo
…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