program story

배치 파일의 인쇄 시간 (밀리 초)

inputbox 2020. 12. 10. 19:55
반응형

배치 파일의 인쇄 시간 (밀리 초)


Windows 배치 파일에 시간 (ms)을 어떻게 인쇄합니까?

배치 파일에서 줄 사이에 걸리는 시간을 측정하고 싶지만 Windows의 "시간 / 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배치 파일의 시작 부분에 a 넣고 !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

어쩌면 이 도구를 도울 수? 시간을 반환하지는 않지만 명령에 걸리는 시간을 측정하는 좋은 도구입니다.


% TIME %는 자정 이후 H : MM : SS, CS 형식이므로 센티 초로의 변환이 작동하지 않습니다. 오전 6시 46 분에 패트릭 커프의 게시물을 보면 나 뿐만이 아닌 것 같습니다.

그러나이 줄을 사용하면 문제를 쉽게 해결할 수 있습니다.

if " "=="%StartZeit:~0,1%" set StartZeit=0%StartZeit:~-10%
if " "=="%EndZeit:~0,1%" set EndZeit=0%EndZeit:~-10%

좋은 영감을 주셔서 감사합니다! 나는 mplayer, ffmpeg, sox 스크립트에서 그것을 사용하여 단지 재미로 오래된 PocketPlayers에 대한 내 미디어 파일을 포주하고 싶습니다.


당신이하고 싶은 일에주의를 기울여야합니다.

배치에는 날짜와 시간 을 나타내는 내부 변수가 있습니다. % DATE % % TIME % . 그러나 그들은 Windows 로케일에 의존합니다.

%Date%:

  • dd.MM.yyyy
  • dd.MM.yy
  • d.M.yy
  • dd/MM/yy
  • yyyy-MM-dd

%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.

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%.

I am using the wmic windows builtin utility to request such things (put it in a script file):

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:

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.

If you want to continue use this then you can write something like this (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:

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%

참고URL : https://stackoverflow.com/questions/605522/print-time-in-a-batch-file-milliseconds

반응형