在 Windows 中运行批处理文件时,显示 %variables% 为空白/缺失

问题描述

好的,在我的 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...