1

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%
aschipfl
  • 33,626
  • 12
  • 54
  • 99
David Lago
  • 307
  • 1
  • 3
  • 11
  • Please elaborate on "not working"! I think you ran into the problem derived from Unicode-to-ANSI conversion of the `wmic` output by `for /F` which leaves orphaned carriage-return characters that disturbs proper text parsing; take a look at this post for a solution: [Parsing the output of wmic in shell script](http://stackoverflow.com/a/19351263). – aschipfl Sep 01 '16 at 10:34

2 Answers2

5

You can do something like that with formatting the output of WMIC with For / Do loop like that :

@echo off
Call :GetTotalPhysicalMemory
echo TotalPhysicalMemory = %MEM% & pause
exit
::***********************************************
:GetTotalPhysicalMemory
for /f "tokens=2 delims==" %%a in ('
    wmic computersystem get TotalPhysicalMemory /value
') do for /f "delims=" %%b in ("%%a") do (
    Set "MEM=%%b" 
)
exit /b
::***********************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
1

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.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • Actually, the command was edited to simplify the question. The intention was: 'wmic computersystem get TotalPhysicalMemory | findstr -v "Total"' And the result of the script for my (tested in 4 servers): G:\>get_numphycpu.bat CPU: 6 MEM: – David Lago Sep 01 '16 at 10:44
  • By the way, thank you for the answer. It worked, and I understood why :) – David Lago Sep 01 '16 at 10:47
  • @DavidLago you can also check my answer with formatting the output of WMIC with `For / Do` loop – Hackoo Sep 01 '16 at 10:58
  • The third line is definitely *not* empty as such would be skipped be `for /F`, but that line contains an orphaned carriage-return character; also the other two lines may have carriage-return characters appended; the *only reliable method* of getting rid of these characters without a temporary file is to use *two nested `for /F` loops*, like illustrated in these posts: [Text garble in batch script for wmic command](http://stackoverflow.com/a/25604222), [For /F with wmic unwanted output](http://stackoverflow.com/a/34695095) and [Hackoo's answer](http://stackoverflow.com/a/39269615) here. – aschipfl Sep 01 '16 at 16:40