logging - Error counter (batch) -


i have assembled batch script (from pieces found on stackoverflow, of course) searches "error:" string in log file , writes down previous non-error line.

setlocal enabledelayedexpansion  set numbers= /f "delims=:" %%a in ('findstr /i /n /c:"error:" %mainlog%') (     set /a before=%%a-1, after=%%a+1     set "numbers=!numbers!!before!: !before!: " )  (for /f "tokens=1* delims=:" %%a in ('findstr /n "^" %mainlog%  ^|findstr  /b "%numbers%" ^| findstr /v /c:"error:"') echo %%b) > %outputfile% 

the idea log file:

07:19:44     generating software report number 1 07:19:44     generating software report number 2 07:19:44     generating software report number 3 07:37:56 error: not find p(reportswizard_closex.png) s: 0.7 t:     72,-2 within 100 screen searches 07:47:08 error: not find p(reusablepickobject_quick_search.png) s: 0.7 t: -10,0 within 100 screen searches 07:56:24 error: not find p(reusablepickobject_clickstatus.png) s: 0.7 t: -37,17 within 100 screen searches 08:05:54 error: not find p(reusablepickobject_clickstatus.png) s: 0.7 t: -37,17 within 100 screen searches 07:19:44     generating software report number 4 07:19:44     generating software report number 5 07:19:44     generating software report number 6 07:37:56 error: not find p(reportswizard_closex.png) s: 0.7 t: 72,-2 within 100 screen searches 07:47:08 error: not find p(reusablepickobject_quick_search.png) s: 0.7 t: -10,0 within 100 screen searches 07:19:44     generating software report number 7 07:19:44     generating software report number 8 07:19:44     generating software report number 9 

it extracts lines:

07:19:44     generating software report number 3 07:19:44     generating software report number 6 

it might not perfect works far, , improve in order count how many error logs written under non-error logs this:

07:19:44    generating software report number 3 (4 errors) 07:19:44    generating software report number 6 (2 errors)  

and that's it... basically, need add brackets error count, , have no idea how , worth of effort or possible in batch. thank ideas.

another approach: every line, count consecutive error lines , print line before errors counter (explanations in code):

@echo off setlocal enabledelayedexpansion  set "last=" rem every line do: /f "delims=" %%i in (test.log) (   rem contain "error:"?   echo %%i|find "error:" >nul && (     rem yes, increase counter:     set /a count+=1    ) || (      rem no, print last "non error" line , counter:     if !count! gtr 0 echo !last! ^(!count! errors^)     rem reset counter next error block:     set count=0     rem save last line:     set "last=%%i"   )  )  

by removing if !count! gtr 0 you'll every report number of errors


Comments