Batch file WMIC command output as variable issue

1

I am trying to get an output from a WMIC command to set as variable, For some reason it does not work. Can someone assist. This is what I am trying to do

for /f "tokens=*" %q IN (' WMIC /Node^:Comp1 PATH Win32_UserProfile WHERE LocalPath="C:\\users\\ABC1" GET Status ^| find /V "Status" ') do set pat= %q

Is there something wrong that I am doing?

NirmalKD

Posted 2018-03-05T23:54:44.787

Reputation: 11

C:\Windows\System32>for /F "tokens=*" %q in (' WMIC /node:Comp1 PATH win32_UserProfile WHERE LocalPath="C:\users\ABC1" GET Status ^| Find /V "Status" ') do set pro= %q Invalid Verb. – NirmalKD – 2018-03-05T23:58:59.933

Managed to get it working with this command – NirmalKD – 2018-03-06T03:59:12.233

C:\Windows\System32>for /F "tokens=*" %a IN (' WMIC /node:Comp1 PATH win32_UserProfile WHERE "LocalPath='C:\users\ABC1'" GET Status ^| Find /V "Status" ^| Find /V "" ') do set prop=%a

:\Windows\System32>set prop=0

:\Windows\System32>set prop= – NirmalKD – 2018-03-06T03:59:43.123

Any Idea how I can remove all the Spaces from the output of this command – NirmalKD – 2018-03-06T04:00:04.657

I have tried googling it and found that most answers are a little complicated. – NirmalKD – 2018-03-06T04:00:36.463

Can you tell me the exact command within the FOR loop you are using that gives you the expected result with the spaces or whatever? – Pimp Juice IT – 2018-03-06T04:14:44.053

for /F "tokens=*" %a IN (' WMIC /node:Comp1 PATH win32_UserProfile WHERE "LocalPath='C:\users\ABC1'" GET Status ^| Find /V "Status" ^| Find /V "" ') do set prop=%a – NirmalKD – 2018-03-06T04:49:30.143

You got Dave's suggestion to work for you correct? Consider accepting his answer if so by checking the little gray check mark to the upper left of Dave's answer and turn it green to give him and yourself some credit. – Pimp Juice IT – 2018-03-09T17:23:48.597

Answers

1

The command executed by FOR /F goes through extra parsing that converts all unquoted/unescaped cmd.exe token delimiters into spaces. So WHERE LocalPath="value" becomes
WHERE LocalPath "value".

You can escape the =

for /f "tokens=*" %q IN (
  'WMIC /Node^:Comp1 PATH Win32_UserProfile WHERE LocalPath^="C:\\users\\ABC1" GET Status ^| find /V "Status" '
) do set pat= %q

Or you can enclose the entire WHERE clause in double quotes and then use single quotes for the value (this is my preferred way to write WHERE clauses with WMIC when used with FOR /F)

for /f "tokens=*" %q IN (
  'WMIC /Node^:Comp1 PATH Win32_UserProfile WHERE "LocalPath='C:\\users\\ABC1'" GET Status ^| find /V "Status" '
) do set pat= %q

I don't think the : really needs to be escaped, but I don't see how it can do any harm either.

You can run into the same problem when selecting multiple values with WMIC in a for /F - the unquoted/unescaped commas are turned into spaces. But in these cases quoting is not an option - you must escape the commas.

Something like this will not work:

for /f "delims=" %%A in ('wmic ...... get value1,value2,value3') do ...

You must escape the commas:

for /f "delims=" %%A in ('wmic ...... bet value1^,value2^,value3') do ...

EDIT

Well actually, there is a trick that sometimes allows you to completely eliminate all escaping. Since the command executed by FOR /F is executed via CMD /C, you can take advantage of the fact that CMD /C will strip enclosing double quotes.

So something like the following will work without any escapes as long as the keyword doesn't have any characters that need escaping (it isn't quoted during the first round of parsing)

for /f "delims=" %%A in (
  '"wmic .... where this='x' and that='y' get value1,value2,value3 | find "keyword"'
) do ...

dbenham

Posted 2018-03-05T23:54:44.787

Reputation: 9 212