Windows 批处理文件中的计时器
有人可以向我指出一种将计时器添加到 Windows 批处理文件中的方法吗?我需要跟踪我的批次从开始运行的时间。
Can someone point me to a way of adding a timer to a Windows batch file? I need to track the time my batch runs from start.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
重构了计算经过时间的代码:
可重用代码:
调用代码:
请注意“1”前缀,以避免将零填充值解释为八进制值(08 和 09)的错误,并使用适当的常量进行更正。由于这可能会令人难以理解核心计算,因此单行
for
语句的替代方案如下所示:Refactored the code to calculate the elapsed time:
Reusable code:
Calling code:
Note the "1"-prefix to avoid interpretation errors of zero-padded values as octal values (08 and 09), and the correction with the appropriate constant. As this might be confusing to understand the core calculation, an alternative to the one-line
for
statement would be something like the following:一个简约的版本,经过的时间以秒为单位。
A minimalistic version with elapsed time in seconds.
据我所知,没有可以在批处理文件中使用的明确的“计时器”命令 - 但是有一个相当简单的解决方案。在批处理文件的顶部记录当前时间(开始时间),在批处理文件的末尾记录当前时间(结束时间),然后比较两者。像这样的东西应该可以做到这一点(它确实看起来过于复杂,但它可以处理大多数时间的怪癖):
或者#1:Windows Server 2k3资源工具包包括timeit.exe。您可以使用它来显示脚本的各种性能统计数据。
或者#2:您可以比上面发布的脚本简单得多并执行以下操作:
但是,这不会为您提供以秒为单位的执行时间。
As far as I know there is no explicit 'timer' command you can use in batch files - however there is a fairly simple solution. At the top of your batch file record the current time (start time) at the end of the batch file record the current time (end time) and then compare the two. Something like this should do it (it does seem overly complicated but it handles most quirks of time):
Alternatively #1 : The Windows Server 2k3 resource kit includes timeit.exe. You can use this to display various performance stats for your script.
Alternatively #2 : You could go much simpler than the script posted above and do this:
However, this won't give you execution time in seconds.
像这样构造您的程序:
这是 TIMER.BAT 程序的代码。将其另存为路径中某处的单独文件。
Structure your program like this:
Here is the code for the TIMER.BAT program. Save it as a seperate file somewhere in your path.
这个计时器非常准确,从 5 开始倒数。不过,我不确定如何将其添加到代码中。注意:这是一个非常基本的批处理计时器,其他答案可能更好,但这只是使用简单代码设计您自己的批处理计时器的快速方法。
另外,要更改此计时器的开始时间,只需将
set /a time=5
更改为set /a time=(无论您想开始的时间)
This timer is pretty accurate, and counts down from 5. I'm not sure how you could add this into your code though. Note:This is a very basic batch timer, and the other answers are probably better, but this is just a quick way to design your own batch timer with simple code.
Also to change the time this timer starts at, just change
set /a time=5
toset /a time=(whatever time you want to start at)
你可以试试这个(更短..)
You can try this one(Shorter..)
我认为最好的选择是使用 powershell,因为
这会打印经过的时间(以毫秒为单位)
I think the best option will be to use powershell as
this will print the time passed in milliseconds