尝试连续报告CPU时钟速度 [英] Attempting to report CPU clock speed continuously in batch
问题描述
我正在尝试制作CPU时钟速度跟踪器,但是我需要一些有关代码的帮助.
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
你能帮我吗?
推荐答案
wmic
的行尾很丑陋(另外一个CR
),经常会引起麻烦.就您而言,它可以为您完成工作:
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
您应该将wmic
输出过滤到一行(wmic
也返回一些空行)
(我选择了另一个值(LoadPercentage)来使事情发生)
You should filter wmic
output to one line (wmic
returns some empty lines too)
(I choosed another value (LoadPercentage) to let something happen)
我将值(包括附加的CR
)分配给变量,然后将[variable minus CR]写入三个空格和[value
的最后一个字符](即CR
).
当新值比旧值短时,必须用三个空格覆盖其余字符.
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.
这篇关于尝试连续报告CPU时钟速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!