windows 批处理文件中的打印时间(毫秒)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/605522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print time in a batch file (milliseconds)
提问by Assaf Lavie
How do I print the time (in ms) in a Windows batch file?
如何在 Windows 批处理文件中打印时间(以毫秒为单位)?
I want to measure the time that passes between lines in my batch file, but Windows's "time /T" does not print milliseconds.
我想测量批处理文件中各行之间经过的时间,但 Windows 的“time /T”不打印毫秒。
回答by Patrick Cuff
%time%
should work, provided enough time has elapsed between calls:
%time%
应该可以工作,前提是通话之间经过了足够的时间:
@echo OFF
@echo %time%
ping -n 1 -w 1 127.0.0.1 1>nul
@echo %time%
On my system I get the following output:
在我的系统上,我得到以下输出:
6:46:13.50
6:46:13.60
6:46:13.50
6:46:13.60
回答by Joey
If you're doing something like
如果你正在做类似的事情
for /l %%i in (1,1,500) do @echo %time%
or
或者
if foo (
echo %time%
do_something
echo %time%
)
then you could simply put a setlocal enabledelayedexpansion
at the beginning of your batch file and use !time!
instead of %time%
which gets evaluated on execution, not on parsing the line (which includes complete blocks enclosed in parentheses).
那么您可以简单地将 asetlocal enabledelayedexpansion
放在批处理文件的开头,并使用!time!
而不是%time%
在执行时进行评估,而不是在解析行(包括括号中的完整块)时进行评估。
回答by nusi
Below batch "program" should do what you want. Please note that it outputs the data in centiseconds instead of milliseconds. The precision of the used commands is only centiseconds.
下面的批处理“程序”应该做你想做的。请注意,它以厘秒而不是毫秒为单位输出数据。使用的命令的精度只有厘秒。
Here is an example output:
这是一个示例输出:
STARTTIME: 13:42:52,25
ENDTIME: 13:42:56,51
STARTTIME: 4937225 centiseconds
ENDTIME: 4937651 centiseconds
DURATION: 426 in centiseconds
00:00:04,26
Here is the batch script:
这是批处理脚本:
@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
回答by Stefan
回答by Mae
%TIME% is in the format H:MM:SS,CS after midnight and hence conversion to centiseconds >doesn't work. Seeing Patrick Cuff's post with 6:46am it seems that it is not only me.
%TIME% 在午夜之后采用 H:MM:SS,CS 格式,因此转换为厘秒 > 不起作用。早上 6 点 46 分看到 Patrick Cuff 的帖子,似乎不仅仅是我。
But with this lines bevor you should will fix that problem easy:
但是有了这条线,你应该可以轻松解决这个问题:
if " "=="%StartZeit:~0,1%" set StartZeit=0%StartZeit:~-10%
if " "=="%EndZeit:~0,1%" set EndZeit=0%EndZeit:~-10%
Thanks for your nice inspiration! I like to use it in my mplayer, ffmpeg, sox Scripts to pimp my mediafiles for old PocketPlayers just for fun.
谢谢你的好灵感!我喜欢在我的 mplayer、ffmpeg、sox 脚本中使用它来为旧的 PocketPlayers 拉皮条我的媒体文件只是为了好玩。
回答by Andry
You have to be careful with what you want to do, because it is not just about to get the time.
你必须小心你想做的事情,因为这不仅仅是为了得到时间。
The batch has internal variables to represent the date and the tme: %DATE% %TIME%. But they dependent on the Windows Locale.
该批次具有内部变量来表示日期和时间:%DATE% %TIME%。但它们依赖于 Windows Locale。
%Date%:
%日期%:
- dd.MM.yyyy
- dd.MM.yy
- d.M.yy
- dd/MM/yy
- yyyy-MM-dd
- 年月日
- 年月日
- dMyy
- 日/月/年
- yyyy-MM-dd
%TIME%:
%TIME%:
- H:mm:ss,msec
- HH:mm:ss,msec
- 高:分:秒,毫秒
- 时:分:秒,毫秒
Now, how long your script will work and when? For example, if it will be longer than a day and does pass the midnight it will definitely goes wrong, because difference between 2 timestamps between a midnight is a negative value! You need the date to find out correct distance between days, but how you do that if the date format is not a constant? Things with %DATE%and %TIME%might goes worser and worser if you continue to use them for the math purposes.
现在,您的脚本将工作多长时间以及何时?例如,如果它比一天长并且确实过了午夜,它肯定会出错,因为午夜之间的 2 个时间戳之间的差异是负值!您需要日期来找出天之间的正确距离,但是如果日期格式不是常数,您如何做到这一点?如果您继续将它们用于数学目的,带有%DATE%和%TIME% 的事情可能会变得越来越糟。
The reason is the %DATE%and %TIME%are exist is only to show a date and a time to user in the output, not to use them for calculations. So if you want to make correct distance between some time values or generate some unique value dependent on date and time then you have to use something different and accurate than %DATE%and %TIME%.
原因是%DATE%和%TIME%的存在只是为了在输出中向用户显示日期和时间,而不是将它们用于计算。因此,如果您想在某些时间值之间建立正确的距离或根据日期和时间生成一些唯一值,那么您必须使用与%DATE%和%TIME%不同且准确的内容。
I am using the wmic windows builtin utility to request such things (put it in a script file):
我正在使用 wmic windows 内置实用程序来请求此类内容(将其放入脚本文件中):
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE`) do if "%%i" == "LocalDateTime" echo.%%j
or type it in the cmd.exe console:
或在 cmd.exe 控制台中输入:
for /F "usebackq tokens=1,2 delims==" %i in (`wmic os get LocalDateTime /VALUE`) do @if "%i" == "LocalDateTime" echo.%j
The disadvantage of this is a slow performance in case of frequent calls. On mine machine it is about 12 calls per second.
这样做的缺点是在频繁调用的情况下性能较慢。在我的机器上,每秒大约有 12 个调用。
If you want to continue use this then you can write something like this (get_datetime.bat):
如果你想继续使用它,那么你可以写这样的东西(get_datetime.bat):
@echo off
rem Description:
rem Independent to Windows locale date/time request.
rem Drop last error level
cd .
rem drop return value
set "RETURN_VALUE="
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if "%%i" == "LocalDateTime" set "RETURN_VALUE=%%j"
if not "%RETURN_VALUE%" == "" (
set "RETURN_VALUE=%RETURN_VALUE:~0,18%"
exit /b 0
)
exit /b 1
Now, you can parse %RETURN_VALUE%somethere in your script:
现在,您可以在脚本中解析%RETURN_VALUE%某处:
call get_datetime.bat
set "FILE_SUFFIX=%RETURN_VALUE:.=_%"
set "FILE_SUFFIX=%FILE_SUFFIX:~8,2%_%FILE_SUFFIX:~10,2%_%FILE_SUFFIX:~12,6%"
echo.%FILE_SUFFIX%
回答by igor
To time task in CMD is as simple as
在 CMD 中计时任务很简单
echo %TIME% && your_command && cmd /v:on /c echo !TIME!