开发者

Average timestamp in batch file

so in short my script currently outputs a csv with the log name, time stamp (10:38:52) and the message (Verification: SUCCESS and SendResponse: Response message).

I now need for this script to be able to calculate and output the time difference between the time stamp on the first message and the timestamp on the second message and provid开发者_C百科e an average 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 and does pull out just the following lines

[20110314T103852][EMVLib][5056][I000000]:  Verification: SUCCESS

[20110314T103902][CSV][3232][D000000]: SendResponse: Response message

These search strings are defined by file searchstrings.txt

Thanks for any help on this!


you have to parse the timestamp and calculate the time value in, say, seconds.

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

EDIT: I discovered that you cannot pass the string directly, so I have modified 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 may then use it to calculate the time difference like this

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 HELP SET and HELP CALL information.


The code of PA shows the idea, but as it's batch there is no simple solution :-)

The getTime function will fail for 14% of the timestamps of a day.
Why? Because batch handles all numbers with leading zeros as octals, so a 08 and a 09 isn't convert to 8 or 9, it simply fails.

There exists a workaround with prefix a 1 and use only the remainder.
set /a "hh=1!var:0,2! %% 100"
08 -> 108 mod 100 -> 8

So the complete batch could 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!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜