I am having some trouble writing a script that will find a string iff the string occurs on higher line number than a previously found string.
FIND /N "BEGIN" "TEST_LOG.txt" && FIND "[ERROR" TEST_LOG.txt && EXIT /B 255
This string will first return an exit code of 255 if the log has been written (“BEGIN” will always appear) and the second string “[ERROR” is found.
Because it is desirable to append to the log file I would like to ONLY look for “[ERROR” on line numbers > the max line number discovered in the FIND /N “BEGIN” statement.
For example, with a log file like this:
BEGINNING 12:03:45.17
BEGINNING 12:03:45.17
BEGINNING 12:03:45.17
BEGINNING 12:03:45.17
[ERROR
I would expect the code to exit with error code 255.
But, with a log file like this I would not:
BEGINNING 12:03:45.17
BEGINNING 12:03:45.17
[ERROR
BEGINNING 12:03:45.17
BEGINNING 12:03:45.17
ALL IS WELL!
I look forward to your replies and thank you in advance of your help.
You simply need to establish the highest line number that each strings appears, and exit if the [ERROR string line number is greater than for BEGIN. Use a FOR /F loop to process each matching line from FINDSTR /N and set a variable to the line number. The last match processed wins, and will be the highest occurrence. You need two loops – one for each search string.
I used the FINDSTR /B option to only match if the string appears at the beginning of the line. Obviously you can remove that option if it is not your requirement.