Attempting to report CPU clock speed continuously

2019-08-19 03:36发布

问题:

I am trying to make a CPU clock speed tracker, but I need some help on the code.

setlocal enableextensions enabledelayedexpansion
:a
for /f %%a in ('copy /Z "%~dpf0" nul') do set "ASCII_13=%%a"
set /p "=!ASCII_13!" <NUL
wmic cpu get CurrentClockSpeed
goto a

I'm interested in getting something like this:

CurrentClockSpeed
2401

not this (the code I'm trying to use outputs this):

CurrentClockSpeed
2401


CurrentClockSpeed
2401


CurrentClockSpeed
2401


CurrentClockSpeed
2401

Can you please help me?

回答1:

wmic has an ugly line ending (an additional CR), that regularily makes trouble. In your case, it does the work for you:

@echo off
setlocal EnableDelayedExpansion
:loop
for /f "delims=" %%a in ('wmic cpu get LoadPercentage /value ^|find "="') do (
  set "value=%%a"
  <nul set /p "=!value:~0,-1!  !value:~-1!"
)
goto :loop

You should filter wmic output to one line (wmic returns some empty lines too)
(I choosed another value (LoadPercentage) to let something happen)

I assign the value (including the additional CR) to a variable, then write [variable minus CR] three spaces and the [last char of value] (which is the CR). The three spaces are neccessary to overwrite remaining characters, when the new value is shorter than the old one.



回答2:

@echo off
setlocal EnableDelayedExpansion

for /f %%a in ('copy /Z "%~dpf0" nul') do set "ASCII_13=%%a"
echo CurrentClockSpeed

:a
for /F "tokens=1,2" %%a in ('wmic cpu get CurrentClockSpeed') do if "%%b" neq "" set "value=%%a"
set /p "=%value%    !ASCII_13!" <NUL
goto a