Need to search timestamps within the lines of a file using Windows XP/7 batch (revisited)

369 views Asked by At

I need to search these style of lines:

2013/10/05 01:32:38 L501/G029 Indicate 95 19 18 04 03 
2013/10/05 01:32:54 L501/G031 L3 Indicate update 04 00 0B 
2013/10/05 01:32:54 L501/G031 Indicate 04 00 0B 
2013/10/05 01:33:03 L501/G031 L3 Indicate update 04 00 09 
2013/10/05 01:33:03 L501/G031 Indicate 04 00 09 
2013/10/05 01:33:03 L501/G013 Recall 
2013/10/05 01:33:03 L501/G013 L3 Recall   
2013/10/05 01:33:03 L501/G013 L3 Indicate update 95 15 05 00 00 00 00 00 
2013/10/05 01:33:03 L501/G013 Indicate 95 15 05 00 00 00 00 00

using the timestamps (not dates). I have come up with the code below which the echo shows it would be working correctly if the comparison format was correct. The echo shows me the time variables are not being parsed correctly. Here is the echo:

F:\TraceFilter>(if 01 GEQ 010600 if 01 LEQ 032400 echo "2013/10/05 01:33:03 L501/G031 L3 Indicate update 04 00 09 " 1>>F:\TraceFilter\results.txt )

My Code:

::@echo off
set /p hour_start=Start Hour:
set /p minute_start=Start Minute:
set /p hour_stop=Stop Hour:
set /p minute_stop=Stop Minute:
set "time_start=%hour_start%%minute_start%00"
set "time_stop=%hour_stop%%minute_stop%00"
echo %time_start%
echo %time_stop%
pause
for /f "usebackq tokens=1,2*" %%a in ("F:\TraceFilter\log\OCG3102_20131005.log") do (
    for /f "tokens=1-3 delims=: " %%A in ("%%b") do (
    if %%A geq %time_start% if %%A leq %time_stop% echo "%%a %%b %%c">> F:\TraceFilter\results.txt
    ))
pause

Any help is appreciated, even its not pertaining directly to my question. And yes, I AM doing batch, no VBS or VBA suggestions at this point, please. My files are thousands of lines long and I need to be able to extract a specific time frame. Each file is for one day, so the date stamps are the same and therefore ignored. At this point the batch is stored in the same relative path as the logs and results, but when in production, it will be calling out to multiple servers. (I only mention this for anyone that may recommend that I don't need the path info...)

P.S. One other thing: Once the time filter portion works, I will be replacing the "echo...>>..." command with a findstr command so I can filter further based on "Lxxx/Gxxx" field as seen in the example lines. I am thinking something like this:

if %%A geq %time_start% if %%A leq %time_stop% echo "%%a %%b %%c"> time_match |  findstr /c:"G013" "%time_match%">> F:\TraceFilter\results.txt

Does this look correct?

Thanks guys!!

**New code:

echo %time%>>F:\TraceFilter\results.txt
for /f "usebackq tokens=1,2*" %%a in  ("F:\TraceFilter\log\OCG3102_20131005.log") do (
    for /f "tokens=1-3 delims=: " %%A in ("%%b") do (
        if %%A%%B%%C geq %time_start% if %%A%%B%%C leq %time_stop% do (
        echo %%a %%b %%c> F:\TraceFilter\temp.txt
        findstr /i "G007" "F:\TraceFilter\temp.txt">> F:\TraceFilter\results.txt
        if %%A%%B%%C gtr %time_stop% goto end
        )
        )
        )

**Second edit of code:

echo %time%>>F:\TraceFilter\results.txt
(
    for /f "usebackq tokens=1,2*" %%a in ("F:\TraceFilter\log\OCG3102_20131005.log") do (
        for /f "tokens=1-3 delims=: " %%A in ("%%b") do (
            if %%A%%B%%C gtr %time_stop% goto filter
            if %%A%%B%%C geq %time_start% echo %%a %%b %%c
        )
    )
)> F:\TraceFilter\temp.txt

:filter
findstr /i "G007" "F:\TraceFilter\temp.txt">> F:\TraceFilter\results.txt

**Works beautifully!

1

There are 1 answers

2
Magoo On BEST ANSWER
if %%A%%B%%C geq %time_start% if %%A%%B%%C leq %time_stop% echo "%%a %%b %%c">> F:\TraceFilter\results.txt

should get you a step closer. ALL batch variables are strings and can be er, strung together.


Further:

echo %time%>>F:\TraceFilter\results.txt
(
 for /f "usebackq tokens=1,2*" %%a in  ("F:\TraceFilter\log\OCG3102_20131005.log") do (
   for /f "tokens=1-3 delims=: " %%A in ("%%b") do (
     if %%A%%B%%C gtr %time_stop% goto filter
     if %%A%%B%%C geq %time_start% echo %%a %%b %%c
   )
 )
)> F:\TraceFilter\temp.txt
:filter
findstr /i "G007" "F:\TraceFilter\temp.txt"> F:\TraceFilter\results.txt

should be faster.

[Edit 20131202 T0006Z : Stray DO removed]

As soon as a greater-than-stop time is found, goto :filter - as you had - but this allows the next IF to be simplified as time must be <=stop-time.

Parenthesising and redirecting to a new file means that you aren't re-creating a new one-line file for every hit-in-range. Fittering at the end means that FINDSTR is only invoked once.