Average timestamp in a batch file

so in short my script is currently outputting csv with the log name, timestamp (10:38:52) and message (Verification: SUCCESS and SendResponse: Response message).

Now I need this script to be able to calculate and output the time difference between the timestamp on the first message and the timestamp in the second message and provide the average value at the bottom of the csv.

@echo off setlocal EnableDelayedExpansion echo."Location","Date Time","Result","diff" > output2.csv ( for %%a in (z:\logdir\*.log) do ( for /f "tokens=1,2,3,4,5,6,7,8,9,10 delims=[+]" %%B in ('findstr /g:searchstrings.txt ^< %%a') do ( set var=%%B set var1=!var:~-6! echo."%%a","!var1!","%%B","%%F","timetaken" ) ) ) >> output2.csv 

the log file contains many entries, but this script should only call the following lines

 [20110314T103852][EMVLib][5056][I000000]: Verification: SUCCESS [20110314T103902][CSV][3232][D000000]: SendResponse: Response message 

These search strings are defined by searchstrings.txt

Thanks for any help with this!

+4
source share
2 answers

The PA code shows the idea, but since it is a package, there is no easy solution :-)

The getTime function will fail with 14% of the timestamps of the day.
What for? Since the package treats all numbers with leading zeros as octal, so 08 and 09 are not converted to 8 or 9, it just fails.

There is a workaround with the prefix a 1 and only the remainder is used.
set /a "hh=1!var:0,2! %% 100"
08 β†’ 108 mod 100 β†’ 8

So a full batch might look like

 @echo off setlocal EnableDelayedExpansion @echo off setlocal EnableDelayedExpansion echo."Location","Date Time","Result" > output2.csv set totalVerifyTime=0 set totalVerifyCount=0 ( for %%a in (n.txt) do ( for /f "tokens=1,5 delims=[+]" %%B in (%%a) do ( set timefield=%%B set timestamp=!timefield:~-6! set /a "seconds=1!timestamp:~-2!%%100 + ( 1!timestamp:~0,2!%%100 * 60 + 1!timestamp:~2,2!%%100 ) * 60" if "%%C"==": Verification: SUCCESS" ( set verifyStart=!seconds! echo."%%a","!timestamp!","%%B","%%C", "!seconds!", "start of verify" ) ELSE ( set /a diff=seconds - verifyStart echo."%%a","!timestamp!","%%B","%%C", "!seconds!", "!diff!" set /a totalVerifyTime+=diff set /a totalVerifyCount+=1 ) ) ) ) set /a "totalAvg=totalVerifyTime/totalVerifyCount" echo avg = !totalAvg! 
+1
source

you need to analyze the timestamp and calculate the time value, for example, seconds.

This code, assuming this format is 20110314T103852 for the passed string in %1 , returns the time value in %2

EDIT: I found that you cannot pass the string directly, so I changed the code

 :gettime set hh=!%1:~9,2! set mm=!%1:~11,2! set ss=!%1:~13,2! set /A %2=hh*3600+mm*60+ss goto :eof 

You can then use it to calculate a temporary difference such as

 set t=%%a call :gettime t t1 ... set t=%%a call :gettime t t2 set /A deltat=t2-t1 

for a more detailed description of the commands used here, check the HELP SET and HELP CALL information.

+2
source

Source: https://habr.com/ru/post/1346247/


All Articles