2

I have the following...

@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

How come I can't set the number in the variable %cpu% it's always blank!

1
  • Thanks a_horse_with_no_name for the re-formatting :) Commented Sep 3, 2013 at 17:33

2 Answers 2

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

WMIC produces an empty line at the end.You can try also with /format:csv at the end of wmic call which will prevent that.

Check also this: http://www.dostips.com/forum/viewtopic.php?f=3&t=4266

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, thank you very much! Appreciate the explanation too!
0

Have not found a solution quite yet but the problem seems to be that

wmic cpu get loadpercentage

has more than your two lines in the output, the for loop iterates over all lines

if i actually echo all sets in the for loop it looks like this

set cpu=1
set cpu=
cpu is 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.