Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

doublequotes in command inside of FOR loop

325 views
Skip to first unread message

Petr Laznovsky

unread,
Mar 11, 2012, 10:35:25 AM3/11/12
to
have FOR loop parsing output of command

for /f "tokens=1 delims=;" %%a in ('GetWMIProperty.exe -class
Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer
-filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL"') do echo %%a

This work fine, but if binary is in path containing space(s) and thus is
enclosed with doublequotes, than does not work.

(set binpath=c:\free utils)
for /f "tokens=1 delims=;" %%a in ('"%binpath%\GetWMIProperty.exe"
-class Win32_NetworkAdapter -property
Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE
AND MacAddress IS NOT NULL"') do echo %%a

After many experiments with doublequotes with no luck my questions is:

Anybody have idea?

http://glsft.free.fr/index.php?option=com_content&task=view&id=70&Itemid=28

L.

Todd Vargo

unread,
Mar 11, 2012, 10:54:29 AM3/11/12
to
Type FOR/? and read about the usebackq option.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

foxidrive

unread,
Mar 11, 2012, 11:30:32 AM3/11/12
to
This fails here with "WMI Error: HRESULT=80041017"

@echo off
GetWMIProperty.exe -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE"
pause




This seems to work, but it still fails with the error message.


@echo off
set binpath=c:\free utils
for /f "tokens=1 delims=;" %%a in ('""%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL""') do echo %%a
pause




--
Mic

Petr Laznovsky

unread,
Mar 11, 2012, 12:51:12 PM3/11/12
to
This for sure work for me, all on one line:

Petr Laznovsky

unread,
Mar 11, 2012, 5:31:33 PM3/11/12
to
I have read this but maybe do not understand correctly. With this
option, I modified my batch this way:

set binpath=c:\free utils
for /f "usebackq tokens=1 delims=;" %%a in
(`"%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property
Name,MacAddress,AdapterType,Manufacturer -filter 'PhysicalAdapter=TRUE
AND MacAddress IS NOT NULL'`) do echo %%a

But this does not work for me, got WMI Error: HRESULT=80041017

Look like this utility REQUIRE "-filter" value inside doublequotes.

L.

foxidrive

unread,
Mar 11, 2012, 6:34:25 PM3/11/12
to
On 12/03/2012 03:51, Petr Laznovsky wrote:

>> This seems to work, but it still fails with the error message.
>>
>>
>> @echo off
>> set binpath=c:\free utils
>> for /f "tokens=1 delims=;" %%a in ('""%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL""') do echo %%a
>> pause
>>

Note the extra set of double quotes at the start and the end.


> This for sure work for me, all on one line:
>
> for /f "tokens=1 delims=;" %%a in ('GetWMIProperty.exe -class
> Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer
> -filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL"') do echo %%a



Both of these fail, without using the for in do. I was just pointing out that the utility seems to have bugs.

GetWMIProperty.exe -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE"
GetWMIProperty.exe -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL"


--
Mic

Petr Laznovsky

unread,
Mar 11, 2012, 7:19:33 PM3/11/12
to
On 11.3.2012 23:34, foxidrive wrote:
> GetWMIProperty.exe -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE"


Both of those work for me, Win7/32bit

C:\Windows\system32>GetWMIProperty.exe -class Win32_NetworkAdapter
-property Name,MacAddress,AdapterType,Manuf
acturer -filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL"

Intel(R) PRO/100+ Management Adapter;00:02:B3:26:42:09;Ethernet 802.3;Intel
Intel(R) PRO/100 VE Network Connection;00:16:76:4C:ED:7F;Ethernet
802.3;Intel
Ralink RT61 Turbo Wireless LAN Card;00:0E:2E:AA:2C:D0;Ethernet
802.3;Ralink Technology Corp.

C:\Windows\system32>GetWMIProperty.exe -class Win32_NetworkAdapter
-property Name,MacAddress,AdapterType,Manuf
acturer -filter "PhysicalAdapter=TRUE"

Intel(R) PRO/100+ Management Adapter;00:02:B3:26:42:09;Ethernet 802.3;Intel
Intel(R) PRO/100 VE Network Connection;00:16:76:4C:ED:7F;Ethernet
802.3;Intel
Hamachi Network Interface;;;LogMeIn, Inc.
Microsoft Virtual WiFi Miniport Adapter;;;Microsoft
Ralink RT61 Turbo Wireless LAN Card;00:0E:2E:AA:2C:D0;Ethernet
802.3;Ralink Technology Corp.
TAP-Win32 Adapter V9;;;TAP-Win32 Provider V9
VMware Virtual Ethernet Adapter for VMnet1;;;VMware, Inc.
VMware Virtual Ethernet Adapter for VMnet8;;;VMware, Inc.
[CommView] Atheros AR5001X+ Wireless Network Adapter;;;TamoSoft


foxidrive

unread,
Mar 11, 2012, 7:37:32 PM3/11/12
to
So? The point is that is doesn't work on all machines. Or is it only for Windows 7?

Did you notice the solution to your problem, btw?



--
Mic

Petr Laznovsky

unread,
Mar 11, 2012, 7:45:04 PM3/11/12
to
I wrote to the author of this utility, but hi does not reply yet...

> Did you notice the solution to your problem, btw?

Do you mean this with doublequotes at the beginning and the end of
string? I check it before few days, but this is not solution if it
generate the error...

L.

foxidrive

unread,
Mar 11, 2012, 8:03:03 PM3/11/12
to
On 12/03/2012 10:45, Petr Laznovsky wrote:
> On 12.3.2012 0:37, foxidrive wrote:
>> On 12/03/2012 10:19, Petr Laznovsky wrote:
>>> On 11.3.2012 23:34, foxidrive wrote:
>>>> GetWMIProperty.exe -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE"
>>>
>>>
>>> Both of those work for me, Win7/32bit
>>>
>> So? The point is that is doesn't work on all machines. Or is it only for Windows 7?
>
> I wrote to the author of this utility, but hi does not reply yet...

Ok. It looks to be a converted batch file to me when viewing the file in a hex viewer, and uses cscript.

>> Did you notice the solution to your problem, btw?
>
> Do you mean this with doublequotes at the beginning and the end of
> string?

Yes.

> I check it before few days, but this is not solution if it
> generate the error...

The error occurs even at the command prompt without using the for in do.

WMI Error: HRESULT=80041017


This page says the following?
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394559%28v=vs.85%29.aspx

WBEM_E_INVALID_QUERY
2147749911 (0x80041017)

Query was not syntactically valid.




--
Mic

Todd Vargo

unread,
Mar 11, 2012, 10:01:55 PM3/11/12
to
I have not tried the GetWMIProperty utility but I think you were
supposed to only insert usebackq and replace the single quotes with back
quotes. I think replacing the second set of double quotes with single
quotes is causing this error. Did you try running it the following way?

(set binpath=c:\free utils)
for /f "usebackq tokens=1 delims=;" %%a in
(`"%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property
Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE
AND MacAddress IS NOT NULL"`) do echo %%a

jeb

unread,
Mar 12, 2012, 4:38:02 AM3/12/12
to
Hi,

I suppose the main porblem is only the space in the path of the
command.
The FOR /F usebackq, has one bug (minimum one), you can't use quotes
to escape the spaces in the command

Samples
Test file in c:\path with spaces\test.bat
----
@echo off
echo #######

This works
FOR /F "usebackq" %%a in (`"c:\path with spaces\test.bat"`) do echo %
%a
This too
FOR /F "usebackq" %%a in (`"c:\path with spaces\test.bat" param=4`) do
echo %%a

But this one fails
FOR /F "usebackq" %%a in (`"c:\path with spaces\test.bat" param="4"`)
do echo %%a

You can get it working with two different ways
Using CALL

FOR /F "usebackq" %%a in (`call "c:\path with spaces\test.bat"
param="4"`) do echo %%a

or with carat escaping
FOR /F "usebackq" %%a in (`c:\path^^ with^^ spaces\test.bat
param="4"`) do echo %%a


jeb

Petr Laznovsky

unread,
Mar 12, 2012, 7:55:11 AM3/12/12
to
On 12.3.2012 3:01, Todd Vargo wrote:
> (set binpath=c:\free utils)
> for /f "usebackq tokens=1 delims=;" %%a in
> (`"%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property
> Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE
> AND MacAddress IS NOT NULL"`) do echo %%a


Got this error:

'c:\free utils\GetWMIProperty.exe" -class Win32_NetworkAdapter -property
Name MacAddress AdapterType Manufacturer -filter "PhysicalAdapter' is
not recognized as an internal or external command, operable program or
batch file.

Petr Laznovsky

unread,
Mar 12, 2012, 7:58:32 AM3/12/12
to
On 12.3.2012 1:03, foxidrive wrote:
> GetWMIProperty.exe -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE"


For me, is does not produce error if run from cmdline:

C:\Windows\system32>GetWMIProperty.exe -class Win32_NetworkAdapter
-property Name,MacAddress,AdapterType,Manufacturer -filter
"PhysicalAdapter=TRUE"

foxidrive

unread,
Mar 12, 2012, 8:13:06 AM3/12/12
to
On 12/03/2012 22:58, Petr Laznovsky wrote:
> On 12.3.2012 1:03, foxidrive wrote:
>> GetWMIProperty.exe -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE"
>
>
> For me, is does not produce error if run from cmdline:


So I see. It is also listing adapters that aren't physical though, right?

There are three virtual adapters there - or doesn't that matter for what you want to do.



> C:\Windows\system32>GetWMIProperty.exe -class Win32_NetworkAdapter
> -property Name,MacAddress,AdapterType,Manufacturer -filter
> "PhysicalAdapter=TRUE"
>
> Intel(R) PRO/100+ Management Adapter;00:02:B3:26:42:09;Ethernet 802.3;Intel
> Intel(R) PRO/100 VE Network Connection;00:16:76:4C:ED:7F;Ethernet
> 802.3;Intel
> Hamachi Network Interface;;;LogMeIn, Inc.
> Microsoft Virtual WiFi Miniport Adapter;;;Microsoft
> Ralink RT61 Turbo Wireless LAN Card;00:0E:2E:AA:2C:D0;Ethernet
> 802.3;Ralink Technology Corp.
> TAP-Win32 Adapter V9;;;TAP-Win32 Provider V9
> VMware Virtual Ethernet Adapter for VMnet1;;;VMware, Inc.
> VMware Virtual Ethernet Adapter for VMnet8;;;VMware, Inc.
> [CommView] Atheros AR5001X+ Wireless Network Adapter;;;TamoSoft


--
Mic

Petr Laznovsky

unread,
Mar 12, 2012, 8:21:13 AM3/12/12
to
On 12.3.2012 13:13, foxidrive wrote:
> On 12/03/2012 22:58, Petr Laznovsky wrote:
>> On 12.3.2012 1:03, foxidrive wrote:
>>> GetWMIProperty.exe -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE"
>>
>>
>> For me, is does not produce error if run from cmdline:
>
>
> So I see. It is also listing adapters that aren't physical though, right?
>
> There are three virtual adapters there - or doesn't that matter for what you want to do.

This is WMI or adapter driver issue. Some virtual adapters are (for some
reason) marked as "Physical" but those adapters usualy does not have
their own MAC address.

I play with WMI query for some time and find working combination of
filter as: "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL" This
display only real physical adapters (network cards)

L

BTW:
-filter parameter is described by author as "standard WQL query"

Petr Laznovsky

unread,
Mar 12, 2012, 8:22:16 AM3/12/12
to
The first solution work for me. As I reading your posts accross the
internet, you are realy DEEP expert in the batch files.

Hats off, jeb.....

Petr Laznovsky

unread,
Mar 12, 2012, 8:52:22 AM3/12/12
to
But there are one more issue, the batch display only first token from
each line, only name of adapter. The rest three tokens are displayed as
empty.

@echo off
(set binpath=c:\free utils)
for /f "usebackq tokens=1-4 delims=;" %%a in (`call
"%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property
Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE
AND MacAddress IS NOT NULL"`) do (echo %%a %%b %%c %%d %%e)

L.

jeb

unread,
Mar 13, 2012, 5:15:09 AM3/13/12
to
What do you get if you add the ALL-token (*) ?
I expect, that you will see then your missed data.

for /f "usebackq tokens=1-4* delims=;" ......

jeb

foxidrive

unread,
Mar 13, 2012, 5:22:52 AM3/13/12
to
On 13/03/2012 20:15, jeb wrote:
>> But there are one more issue, the batch display only first token from
>> each line, only name of adapter. The rest three tokens are displayed as
>> empty.
>>
>> @echo off
>> (set binpath=c:\free utils)
>> for /f "usebackq tokens=1-4 delims=;" %%a in (`call
>> "%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property
>> Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE
>> AND MacAddress IS NOT NULL"`) do (echo %%a %%b %%c %%d %%e)
>>
>> L.
>
> What do you get if you add the ALL-token (*) ?
> I expect, that you will see then your missed data.
>
> for /f "usebackq tokens=1-4* delims=;" ......
>
> jeb

The first post has a link to the exe - it seems legit to me and it is probably an encoded batch file as you can read cscript in the binary data.


How about someone else try it and report back - I tried it under XP but I get the WMI Error: HRESULT=80041017 error.



--
Mic

foxidrive

unread,
Mar 13, 2012, 5:33:26 AM3/13/12
to
This works for me in a Windows 7 VM - the only change to the original code is that I included the CALL keyword as suggested before.

The binary was saved to the desktop so binpath is pointing there.

@echo off
set binpath=%userprofile%\desktop
for /f "tokens=1 delims=;" %%a in ('call "%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL"') do echo %%a
pause


Intel(R) PRO/1000 MT Desktop Adapter





--
Mic

Petr Laznovsky

unread,
Mar 13, 2012, 5:59:15 AM3/13/12
to
if "tokens=*" than

Intel(R) PRO/100+ Management Adapter;%b;%c;%d;%e
Intel(R) PRO/100 VE Network Connection;%b;%c;%d;%e
Ralink RT61 Turbo Wireless LAN Card;%b;%c;%d;%e


If "tokens=1-4" than

Intel(R) PRO/100+ Management Adapter;;;;%e
Intel(R) PRO/100 VE Network Connection;;;;%e
Ralink RT61 Turbo Wireless LAN Card;;;;%e

foxidrive

unread,
Mar 13, 2012, 6:14:30 AM3/13/12
to
This gets the job done in Win7.

@echo off
set binpath=%userprofile%\desktop

"%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL">file.tmp

for /f "tokens=1-4 delims=;" %%a in (file.tmp) do echo %%a %%b %%c %%d
pause

del file.tmp 2>nul

Petr Laznovsky

unread,
Mar 13, 2012, 8:02:55 AM3/13/12
to
I did the same job yesterday, but thanks anyway..

jeb

unread,
Mar 13, 2012, 7:34:16 AM3/13/12
to
But why it doesn't work inside the FOR /F loop?

You could try to change the EOL-character to nothing or to ex. "#".
You could look at the file.tmp, perhaps there are some embedded
control characters like <sub>0x1A.

jeb

foxidrive

unread,
Mar 13, 2012, 9:50:49 AM3/13/12
to
On 13/03/2012 22:34, jeb wrote:
> On 13 Mrz., 11:14, foxidrive <foxidr...@gotcha.woohoo.invalid> wrote:
>> This gets the job done in Win7.
>>
>> @echo off
>> set binpath=%userprofile%\desktop
>>
>> "%binpath%\GetWMIProperty.exe" -class Win32_NetworkAdapter -property Name,MacAddress,AdapterType,Manufacturer -filter "PhysicalAdapter=TRUE AND MacAddress IS NOT NULL">file.tmp
>>
>> for /f "tokens=1-4 delims=;" %%a in (file.tmp) do echo %%a %%b %%c %%d
>> pause
>>
>> del file.tmp 2>nul
>
> But why it doesn't work inside the FOR /F loop?

I think it is a compiled batch file and being extracted and executed, and something in the code is causing this issue.

> You could try to change the EOL-character to nothing or to ex. "#".

I tried that earlier. No change.

> You could look at the file.tmp, perhaps there are some embedded
> control characters like <sub>0x1A.

It's a single line of text with 0d 0a line end.



--
Mic
0 new messages