如何在批处理文件中获取物理和逻辑内核的数量?
How to get the number of physical and logical cores in a batch file?
WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors
得到了我想要的大部分内容,但如何将输出存储到变量中?
此外,如果是双插槽机器,这 return 会是所有组合内核吗?
要获取命令的输出,请使用 for /f
循环:
for /f "delims=" %%a in ('"WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value"') do set /a "_%%a"
set _
解析 wmic
输出有一些怪癖(Unicode,奇怪的行结尾 CRCRLF
),有几种方法可以得到你想要的格式。由于此处的值是纯数字,因此我选择了 set /a
。如果值是字母数字,这将不起作用。
注意:
NumberOfCores
给出的是核心数,而不是物理处理器数。有单核和多核处理器。可以通过以下方式获取物理处理器的数量:
wmic COMPUTERSYSTEM get NumberOfProcessors,NumberOfLogicalProcessors
(请 double-check 的 NumberOfLogicalProcessors
匹配 WMIC CPU
给出的值。我没有多处理器系统可供检查)
for /f "tokens=1,2delims==" %%b in ('"WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value"') do set /a "%%b=%%c" 2>nul
SET numberof
感兴趣的两个 WMIC 报告行的格式为 item=value
,value
是数字,因此可以使用 set /a
。 2>nul
抑制由处理其他行生成的错误消息。
注意还有一个system-established变量NUMBER_OF_PROCESSORS
根据评论修改
@ECHO OFF
SETLOCAL enabledelayedexpansion
For %%c IN (numberof count) DO FOR /F "delims==" %%b In ('set %%c 2^>Nul') DO SET "%%b="
for /f "tokens=1,2delims==" %%b in ('"WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value"') do (
set/a count%%b+=1
set /a "%%b[!count%%b!]=%%c" 2>NUL
)
SET num
pause
for...%%c
行只是清除名称以 numberof
或 count
.
开头的所有现有变量
for...%%b
中的代码解释 name=value
形式的元素的 WMIC
输出,将 name
分配给 %%b
和 value
到 %c
.
首先,它递增(比如)countNumberOfCores
,然后将 %%c
中的值赋给变量 NumberOfCores[currentvalueof"countNumberOfCores"]
如果您碰巧在没有 wmic 的系统上(例如 Windows11 的某些版本),您可以使用 powershell 在批处理脚本中执行此操作:
set "_numCores="
for /f %%g in ('%__APPDIR__%\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -Command "Get-CimInstance -ClassName Win32_Processor -KeyOnly -Property NumberOfCores | Select-Object -ExpandProperty NumberOfCores" 2^>nul') do set "_numCores=%%g"
if defined _numCores echo Number of Cores: %_numCores%
echo Number of Logical Processors: %NUMBER_OF_PROCESSORS%
注意:现在使用 Compo 的有用建议进行了简化。
编辑 2:感谢 Compo 的建议,我做了一些额外的编辑。随意使用他的编码风格。这里的要点是,如果系统没有 WMIC,您可能需要依赖不同的方法来调用 WMI。替代方案可能是 javascript.
WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors
得到了我想要的大部分内容,但如何将输出存储到变量中?
此外,如果是双插槽机器,这 return 会是所有组合内核吗?
要获取命令的输出,请使用 for /f
循环:
for /f "delims=" %%a in ('"WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value"') do set /a "_%%a"
set _
解析 wmic
输出有一些怪癖(Unicode,奇怪的行结尾 CRCRLF
),有几种方法可以得到你想要的格式。由于此处的值是纯数字,因此我选择了 set /a
。如果值是字母数字,这将不起作用。
注意:
NumberOfCores
给出的是核心数,而不是物理处理器数。有单核和多核处理器。可以通过以下方式获取物理处理器的数量:
wmic COMPUTERSYSTEM get NumberOfProcessors,NumberOfLogicalProcessors
(请 double-check 的 NumberOfLogicalProcessors
匹配 WMIC CPU
给出的值。我没有多处理器系统可供检查)
for /f "tokens=1,2delims==" %%b in ('"WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value"') do set /a "%%b=%%c" 2>nul
SET numberof
感兴趣的两个 WMIC 报告行的格式为 item=value
,value
是数字,因此可以使用 set /a
。 2>nul
抑制由处理其他行生成的错误消息。
注意还有一个system-established变量NUMBER_OF_PROCESSORS
根据评论修改
@ECHO OFF
SETLOCAL enabledelayedexpansion
For %%c IN (numberof count) DO FOR /F "delims==" %%b In ('set %%c 2^>Nul') DO SET "%%b="
for /f "tokens=1,2delims==" %%b in ('"WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value"') do (
set/a count%%b+=1
set /a "%%b[!count%%b!]=%%c" 2>NUL
)
SET num
pause
for...%%c
行只是清除名称以 numberof
或 count
.
for...%%b
中的代码解释 name=value
形式的元素的 WMIC
输出,将 name
分配给 %%b
和 value
到 %c
.
首先,它递增(比如)countNumberOfCores
,然后将 %%c
中的值赋给变量 NumberOfCores[currentvalueof"countNumberOfCores"]
如果您碰巧在没有 wmic 的系统上(例如 Windows11 的某些版本),您可以使用 powershell 在批处理脚本中执行此操作:
set "_numCores="
for /f %%g in ('%__APPDIR__%\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -Command "Get-CimInstance -ClassName Win32_Processor -KeyOnly -Property NumberOfCores | Select-Object -ExpandProperty NumberOfCores" 2^>nul') do set "_numCores=%%g"
if defined _numCores echo Number of Cores: %_numCores%
echo Number of Logical Processors: %NUMBER_OF_PROCESSORS%
注意:现在使用 Compo 的有用建议进行了简化。
编辑 2:感谢 Compo 的建议,我做了一些额外的编辑。随意使用他的编码风格。这里的要点是,如果系统没有 WMIC,您可能需要依赖不同的方法来调用 WMI。替代方案可能是 javascript.