当 运行 是 Windows 中的批处理文件时,显示 %variables% 是 blank/missing

Displaying %variables% is blank/missing when running a batch file in Windows

好的,在我的 BAT(批处理)文件中,我正在尝试检测 Windows 机器的物理核心数,然后将其显示给用户。我正在使用 'wmic',我已确认它在命令提示符下正常工作以重新调整正确的信息。但是,当我尝试显示我存储的值时,它只显示空白 space.

比如我运行这个的时候,最后一行只显示“You have physical cores”,并且在“have”和“physical”之间只有一个space,即使那是 %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