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

Time:04-14

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

CodePudding user response:

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.

CodePudding user response:

@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.

  • Related