Get the result of a WMIC command and store it in a variable

David Lago

I have seen some batch scripts working that way, including all around stackoverflow.

My question is simple: Why the MEM part isn't working?

@echo OFF

SET CPU="$CPU"

echo CPU: %NUMBER_OF_PROCESSORS%

FOR /F "delims=" %%i IN ('wmic computersystem get TotalPhysicalMemory') DO set MEM=%%i

echo MEM: %MEM%
MichaelS

That's simple. wmic computersystem get TotalPhysicalMemory outputs three lines of text:

TotalPhysicalMemory
12867309568
<blank line>

So your for-loop does three iteration. In the first one MEM is set to TotalPhysicalMemory, in the second one it's set to 12867309568 and finally it becomes . So your output is empty.

This is quite ugly but will solve your problem:

@echo OFF
setlocal enabledelayedexpansion
SET CPU="$CPU"
echo CPU: %NUMBER_OF_PROCESSORS%
FOR /F "delims= skip=1" %%i IN ('wmic computersystem get TotalPhysicalMemory') DO (
    set MEM=%%i
    goto STOP

)
:STOP
echo MEM: !MEM!

skip=1 will ignore TotalPhysicalMemory and goto STOP will break the loop after the first iteration.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

store the output of powershell command in a variable

From Dev

Run a command in GNU and BSD Makefile and store the result into a variable

From Dev

Store_result and get_result for statement

From Dev

Cannot store result into variable SQL

From Dev

Store column result in variable

From Dev

Get product name from WMIC for a variable in batch

From Dev

Store function result into variable

From Dev

store a result of a command into a variable using csh

From Dev

Assign result of command in a variable

From Dev

how to store result of tail command in variable?

From Dev

Shell script -- how to store curl command WITH variables result into another variable?

From Dev

Store the result of a command in a variable

From Dev

Run a command in GNU and BSD Makefile and store the result into a variable

From Dev

Store the assert failure result in a variable

From Dev

Store the result of a Dynamic Query in a variable

From Dev

store ftp command output in a variable

From Dev

output awk command result to variable

From Dev

store into variable a sudo result

From Dev

How to get BIOS serialnumber with WMIC command

From Dev

Store into variable result of function VBA

From Dev

Store cmd Find result to a variable

From Dev

How to get the result of a grep command on a variable into another variable

From Dev

Store function result into variable

From Dev

store a result of a command into a variable using csh

From Dev

JSP,mysql - How to get and store variable from a fresh query result

From Dev

Sed Fails to store the result into a variable

From Dev

Store Result of SVN Command in Variable

From Dev

awk assign command result to a variable

From Dev

How to store a query result in a variable

Related Related

HotTag

Archive