John Ring
John Ring

Reputation: 57

Set Variable to value of command

I need to set the value of WMIC COMPUTERSYSTEM GET MODEL /Value to a variable. when I run this command I get the results below. Why do I get the extra blank lines?

U:\>for /f "delims=" %i in ('WMIC COMPUTERSYSTEM GET MODEL /Value') do set output=%i
 :\>set output=
 :\>set output=
 :\>set output=Model=HP ProDesk 600 G1 SFF
 :\>set output=
 :\>set output=
 :\>set output=

Upvotes: 2

Views: 1240

Answers (4)

aschipfl
aschipfl

Reputation: 34899

The wmic command produces Unicode output which for /F has problems with, so unwanted artefacts remain. In fact those are orphaned carriage-return characters. The best and most flexible method to overcome this is to nest another for /F loop so that the wmic output is parsed twice.

The following command line is typed directly into the console, the respective output is also shown:

U:\>for /F "delims=" %I in ('wmic ComputerSystem GET Model /VALUE') do @for /F "delims=" %L in ("%I") do set "output=%L"

U:\>set "output=Model=HP ProDesk 600 G1 SFF"

U:\>

To use the above command line in a batch file you need to double the percent signs:

@echo off
for /F "delims=" %%I in ('wmic ComputerSystem GET Model /VALUE') do (
    for /F "delims=" %%L in ("%%I") do set "output=%%L"
)
echo %output%

To strip off the Model= prefix from the value of the variable output, change the script to this:

@echo off
for /F "tokens=1,* delims==" %%I in ('wmic ComputerSystem GET Model /VALUE') do (
    for /F "delims=" %%L in ("%%J") do set "output=%%L"
)
echo %output%

The double-parsing method is actually credited to dbenham -- see his answer to Why is the FOR /f loop in this batch script evaluating a blank line? and also his external article WMIC and FOR /F : A fix for the trailing <CR> problem.

Upvotes: 0

Filipus
Filipus

Reputation: 540

As others have already mentioned, the issue is due to the Unicode output of WMIC. In your case, you can still keep everything on a single line with the following command:

for /f "delims=" %i in ('WMIC COMPUTERSYSTEM GET MODEL /Value^|findstr /i "MODEL"') do @(set output=%i)

Upvotes: 0

jeb
jeb

Reputation: 82202

It's a problem of the WMIC output, that contains carriage return characters.
Therefore the empty lines aren't removed, as they are not completely empty.

The carriage returns are also responsible for the strange output of :> instead of U:>, as the carriage return first moves the cursor to POS1 and then a space deletes the U.

With an extra FOR/F you can strip the carriage return characters

for /f "delims=" %i in ('WMIC COMPUTERSYSTEM GET MODEL /Value') do @(
    for /F "delims=" %X in ("%i") do set output=%X
)

Btw. Normally you want such an expression in a batch file, not at the command line.
Then you have to convert it to

@echo off
for /f "delims=" %%i in ('WMIC COMPUTERSYSTEM GET MODEL /Value') do (
    for /F "delims=" %%X in ("%%i") do set output=%%X
)

Upvotes: 2

JimmyB
JimmyB

Reputation: 12610

Why do I get the extra blank lines?

Because WMIC prints those empty lines in its output, and "FOR /F processing of a text file consists of reading the file, one line of text at a time and then breaking the line up into individual items" (emphasis mine).

So even if you don't define any delimers, each line still is processed individually.

If you don't need that Model= in your output, you can use this:

for /f "delims== tokens=2" %i in ('WMIC COMPUTERSYSTEM GET MODEL /Value') do set output=%i

Upvotes: 0

Related Questions