2

我有以下...

@ECHO OFF
setlocal EnableDelayedExpansion

for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do @echo %%p
ECHO ...this worked
ECHO. 
PAUSE

for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do set cpu=%%p
ECHO cpu is %cpu%
ECHO ...this did not work
ECHO. 
PAUSE

为什么我不能在变量 %cpu% 中设置数字它总是空白!

4

2 回答 2

3
for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do set cpu=%%p & goto :end_for
:end_for

WMIC最后产生一个空行。您也可以/format:csv在 wmic 调用结束时尝试使用,这将防止这种情况发生。

还要检查这个:http ://www.dostips.com/forum/viewtopic.php?f=3&t=4266

于 2013-09-03T17:41:02.140 回答
0

还没有找到解决方案,但问题似乎是

wmic cpu get loadpercentage

输出中有超过两行,for循环遍历所有行

如果我实际上在 for 循环中回显所有集合,它看起来像这样

set cpu=1
set cpu=
cpu is 
于 2013-09-03T17:44:56.037 回答