Tempo di stampa in un file batch (millisecondi)
-
03-07-2019 - |
Domanda
Come si stampa l'ora (in ms) in un file batch di Windows?
Voglio misurare il tempo che passa tra le righe nel mio file batch, ma Windows "time / T" non stampa millisecondi.
Soluzione
% time%
dovrebbe funzionare, a condizione che sia trascorso abbastanza tempo tra le chiamate:
@echo OFF
@echo %time%
ping -n 1 -w 1 127.0.0.1 1>nul
@echo %time%
Sul mio sistema ottengo il seguente output:
6: 46: 13.50
6: 46: 13.60
Altri suggerimenti
Se stai facendo qualcosa del genere
for /l %%i in (1,1,500) do @echo %time%
o
if foo (
echo %time%
do_something
echo %time%
)
allora potresti semplicemente mettere un setlocal enabledelayedexpansion
all'inizio del tuo file batch e usare ! time!
invece di % time%
che viene valutato all'esecuzione, non all'analisi della linea (che include blocchi completi racchiusi tra parentesi).
Sotto batch "programma" dovrebbe fare quello che vuoi. Si noti che genera i dati in centisecondi anziché in millisecondi. La precisione dei comandi utilizzati è solo di centisecondi.
Ecco un esempio di 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
Ecco lo script batch:
@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% è nel formato H: MM: SS, CS dopo mezzanotte e quindi la conversione in centisecondi > non funziona. Vedendo il post di Patrick Cuff alle 6:46, sembra che non sia solo io.
Ma con questo bisello di linee dovresti risolvere facilmente questo problema:
if " "=="%StartZeit:~0,1%" set StartZeit=0%StartZeit:~-10%
if " "=="%EndZeit:~0,1%" set EndZeit=0%EndZeit:~-10%
Grazie per la tua bella ispirazione! Mi piace usarlo nel mio mplayer, ffmpeg, sox Script per magnacciare i miei file multimediali per vecchi PocketPlayer solo per divertimento.
Devi stare attento a ciò che vuoi fare, perché non si tratta solo di ottenere il tempo.
Il batch ha variabili interne per rappresentare la data e l'ora: % DATE%% TIME% . Ma dipendono dalle impostazioni internazionali di Windows.
% Data% :
- dd.MM.yyyy
- GG.MM.AA
- d.M.yy
- gg / mm / aa
- yyyy-MM-dd
% TIME% :
- H: mm: ss, msec
- hh: mm: ss, msec
Ora, per quanto tempo funzionerà la tua sceneggiatura e quando? Ad esempio, se sarà più lungo di un giorno e passerà la mezzanotte andrà sicuramente storto, perché la differenza tra 2 timestamp tra una mezzanotte è un valore negativo! Hai bisogno della data per scoprire la distanza corretta tra i giorni, ma come fai se il formato della data non è una costante? Le cose con % DATE% e % TIME% potrebbero peggiorare sempre di più se continui a usarle per scopi matematici.
Il motivo è che % DATE% e % TIME% esistono solo per mostrare una data e un'ora all'utente nell'output, non per usarle per calcoli. Quindi, se vuoi fare la distanza corretta tra alcuni valori temporali o generare un valore univoco dipendente da data e ora, devi usare qualcosa di diverso e preciso rispetto a % DATE% e % TIME% .
Sto usando l'utility integrata wmic windows per richiedere tali cose (inserirla in un file di script):
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE`) do if "%%i" == "LocalDateTime" echo.%%j
o digitalo nella console cmd.exe:
for /F "usebackq tokens=1,2 delims==" %i in (`wmic os get LocalDateTime /VALUE`) do @if "%i" == "LocalDateTime" echo.%j
Lo svantaggio di questo è una prestazione lenta in caso di chiamate frequenti. Sulla mia macchina sono circa 12 chiamate al secondo.
Se vuoi continuare a usarlo, puoi scrivere qualcosa del genere ( 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
Ora puoi analizzare % RETURN_VALUE% da qualche parte nel tuo 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%