How can I set a WMIC Variable in a batch file?

Dan O.

I created this simple batch file and it sets the variable twice. Once with the answer I'm looking for, and then a second time blank which erases the variable.

for /f "tokens=1 delims==" %%f in ('WMIC PRODUCT WHERE "Name like "%%iTunes%%"" get name') do set "myVar=%%f"
echo %myVar%

It gives me the below result.

C:\Batch>disktest

C:\Batch>for /F "tokens=1 delims==" %f in ('WMIC PRODUCT WHERE "Name like "%iTun
es%"" get name') do set "myVar=%f"

" \Batch>set "myVar=Name

" \Batch>set "myVar=iTunes

" \Batch>set "myVar=

C:\Batch>echo
ECHO is on.

C:\Batch>

So somewhere I'm not escaping correctly with a ^ or I don't know what I'm missing. Can anyone help me out?

This is Windows 7x64 but I'd also like to use it in XP SP3 as well.

Ian

After some experiments with the newly discovered wmic :) I think your problem is that, by default, it will list the results you want along with the title of the column first and following empty line at the end.

To filter the results from wmic, add | FIND "xxx", e.g.,

wmic product where "name like 'xxx'" get name | find "xxx"

This will filter out the line containing the title (which is why you're getting "Name" in your results) and the final blank line.

In fact, on further reflection, you may be able to remove the "where" clause completely, replacing it with the "find", so:

wmic product get name | find "xxx"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how can I set permissions to a batch file mkdir creation

From Dev

How can I set a time-limit on a batch file?

From Dev

How I can write the name of a file from a variable in Batch

From Dev

How to run wmic command in a batch file

From Dev

How to write a batch file to return a WMIC result

From Dev

Batch file - set variable in “if” then "set"

From Dev

Batch file - set variable in “if” then "set"

From Dev

How can I call a batch file within another batch file?

From Dev

What is the WMIC command, and how can I use it?

From Java

How can I echo a newline in a batch file?

From Java

How can I pass arguments to a batch file?

From Dev

How can I convert this vbs into a batch file?

From Dev

How can I run this batch file?

From Dev

wmic uninstall using batch file

From Dev

How can I use return data set a new parameters in batch file?

From Dev

Batch File: How can I extract the substring of a value from a FOR loop using an index variable?

From Dev

How can I retrieve the P4 ROOT variable value in a Windows Batch file?

From Dev

how I can call from batch file a java script function with 2 arguments and return the result to a dos variable

From Dev

Set Bamboo variable in batch file

From Dev

Batch File Set Variable not working

From Dev

Batch File SET variable issue

From Dev

Set Bamboo variable in batch file

From Dev

how to set variable on window batch file for private use only

From Dev

How in batch file set to variable up level path

From Dev

How to set PATH environment variable in batch file only once on Windows?

From Dev

how to set variable on window batch file for private use only

From Dev

How to set PATH environment variable in batch file only once on Windows?

From Dev

How can I set the 'Path' variable in a .desktop file to be relative to the location where the desktop file is located?

From Dev

batch - how can I close a batch file that was started from another batch file

Related Related

  1. 1

    how can I set permissions to a batch file mkdir creation

  2. 2

    How can I set a time-limit on a batch file?

  3. 3

    How I can write the name of a file from a variable in Batch

  4. 4

    How to run wmic command in a batch file

  5. 5

    How to write a batch file to return a WMIC result

  6. 6

    Batch file - set variable in “if” then "set"

  7. 7

    Batch file - set variable in “if” then "set"

  8. 8

    How can I call a batch file within another batch file?

  9. 9

    What is the WMIC command, and how can I use it?

  10. 10

    How can I echo a newline in a batch file?

  11. 11

    How can I pass arguments to a batch file?

  12. 12

    How can I convert this vbs into a batch file?

  13. 13

    How can I run this batch file?

  14. 14

    wmic uninstall using batch file

  15. 15

    How can I use return data set a new parameters in batch file?

  16. 16

    Batch File: How can I extract the substring of a value from a FOR loop using an index variable?

  17. 17

    How can I retrieve the P4 ROOT variable value in a Windows Batch file?

  18. 18

    how I can call from batch file a java script function with 2 arguments and return the result to a dos variable

  19. 19

    Set Bamboo variable in batch file

  20. 20

    Batch File Set Variable not working

  21. 21

    Batch File SET variable issue

  22. 22

    Set Bamboo variable in batch file

  23. 23

    how to set variable on window batch file for private use only

  24. 24

    How in batch file set to variable up level path

  25. 25

    How to set PATH environment variable in batch file only once on Windows?

  26. 26

    how to set variable on window batch file for private use only

  27. 27

    How to set PATH environment variable in batch file only once on Windows?

  28. 28

    How can I set the 'Path' variable in a .desktop file to be relative to the location where the desktop file is located?

  29. 29

    batch - how can I close a batch file that was started from another batch file

HotTag

Archive