问题描述
好的,在我的 BAT(批处理)文件中,我试图检测 Windows 机器的物理内核数量,然后将其显示给用户。我正在使用'wmic',我已经确认它在命令提示符下正常工作以重新调整正确的信息。但是,当我尝试显示我存储的值时,它只显示一个空格。
例如,当我运行这个时,最后一行只显示“你有物理内核”,而“有”和“物理”之间只有一个空格,即使那是 %cpuvar2% 的值应该是显示。我无法弄清楚为什么这不显示。我也试过@ECHO ON,但没有运气。
:labelcpu
@ECHO OFF
REM Intro message to let the user kNow what is happening
ECHO Detecting cpu cores...
REM Command to get the number of physical cores and store as a string var
wmic computersystem get numberoflogicalprocessors> cpuvar
REM Command to remove the first line of junk text from the returned string and save as a new var
MORE +1 cpuvar> cpuvar2
REM Output the number of physical cores to the user
ECHO You have %cpuvar2% physical cores!
系统:Windows 10 专业版,x64
解决方法
这里有一个简单的例子供您学习。
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Echo Detecting CPU cores...
Set "NumCores=0"
For /F EOL^=N %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
ComputerSystem Get NumberOfLogicalProcessors 2^>NUL'
) Do For /F "Tokens=*" %%H In ("%%G") Do Set "NumCores=%%H"
Echo You have %NumCores% physical cores!
Pause