Attempting to report CPU clock speed continuously in batch

…衆ロ難τιáo~ 提交于 2019-12-11 16:09:40

问题


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


来源:https://stackoverflow.com/questions/47187315/attempting-to-report-cpu-clock-speed-continuously-in-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!