如何在Windows批处理文件中打印时间(以毫秒为单位)?
我想测量批处理文件中行之间的时间,但是Windows的"time/T"不会打印毫秒.
%time%
应该工作,只要呼叫之间有足够的时间:
@echo OFF @echo %time% ping -n 1 -w 1 127.0.0.1 1>nul @echo %time%
在我的系统上,我得到以下输出:
6:46:13.50
6:46:13.60
如果你正在做类似的事情
for /l %%i in (1,1,500) do @echo %time%
要么
if foo ( echo %time% do_something echo %time% )
那么你可以简单地setlocal enabledelayedexpansion
在批处理文件的开头放一个!time!
而不是%time%
在执行时使用它来进行评估,而不是解析该行(包括括在括号中的完整块).
批量"程序"下面应该做你想要的.请注意,它以厘秒而不是毫秒输出数据.使用命令的精度只有几厘秒.
这是一个示例输出:
STARTTIME: 13:42:52,25 ENDTIME: 13:42:56,51 STARTTIME: 4937225 centiseconds ENDTIME: 4937651 centiseconds DURATION: 426 in centiseconds 00:00:04,26
这是批处理脚本:
@echo off setlocal rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99 set STARTTIME=%TIME% rem here begins the command you want to measure dir /s > nul rem here ends the command you want to measure set ENDTIME=%TIME% rem output as time echo STARTTIME: %STARTTIME% echo ENDTIME: %ENDTIME% rem convert STARTTIME and ENDTIME to centiseconds set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100) set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100) rem calculating the duratyion is easy set /A DURATION=%ENDTIME%-%STARTTIME% rem we might have measured the time inbetween days if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME% rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds set /A DURATIONH=%DURATION% / 360000 set /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000 set /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100 set /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100) rem some formatting if %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH% if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM% if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS% if %DURATIONHS% LSS 10 set DURATIONHS=0%DURATIONHS% rem outputing echo STARTTIME: %STARTTIME% centiseconds echo ENDTIME: %ENDTIME% centiseconds echo DURATION: %DURATION% in centiseconds echo %DURATIONH%:%DURATIONM%:%DURATIONS%,%DURATIONHS% endlocal goto :EOF