roro
roro

Reputation: 940

How to get the number of physical and logical cores in a batch file on a dual socket machine?

WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors gets me most of what I want, but how do I store the combined output into a variable?

for /f "delims=" %%a in ('"WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value"') do set /a "_%%a"
set _

Works for single socket machines only. The WMIC command returns cpu info on separate lines, i.e.

>WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value

NumberOfCores=24
NumberOfLogicalProcessors=48

NumberOfCores=24
NumberOfLogicalProcessors=48

Upvotes: 0

Views: 131

Answers (2)

Aacini
Aacini

Reputation: 67216

@echo off
setlocal

set /A "_NumberOfCores=_NumberOfLogicalProcessors=0"
for /F "tokens=1,2 delims==" %%a in ('WMIC CPU Get NumberOfCores^,NumberOfLogicalProcessors /value') do if "%%b" neq "" set /A "_%%a+=%%b"

set _

Note that wmic command output lines terminated in CR+CR+LF ASCII characters, even the empty lines, so it is necessary to check if the %%b part exists to avoid to process empty lines.

Upvotes: 2

Styris
Styris

Reputation: 186

This will do it:

for /f "tokens=2,3 delims=," %%a in ('WMIC CPU Get NumberOfCores^,NumberOfLogicalProcessors /value /format:csv') do set "both=Cores=%%a Processors=%%b"

/format:csv changes up the format some, making it easier for us to manipulate, which I did in the above command.

Upvotes: 0

Related Questions