Search code examples
windowsbatch-fileloggingwgetfindstr

Findstr failing to find string "Failures: 0"


I am writing a little batch script on a Windows machine for automating the pulldown of failure messages and screenshots.

I have gotten to the point where once I have a list of urls, I can get what I want. However, I currently need to create these lists of urls by hand. The information I use is in a log file containing the console output of a server running a set of tests and comes in the form:

E:\foo\bar\three\tall\cats\testRunOutput\foobar\foosball\dogstest\Login.html
Successes: 1, Failures: 0

E:\foo\bar\three\tall\cats\testRunOutput\foodbarn\catdog\testoid\ReLogin.html
Successes: 0, Failures: 1

E:\foo\bar\three\tall\cats\testRunOutput\foobar\topdog\dogstest\Login.html
Successes: 1, Failures: 4

What I need is the lines above any line containing a nonzero failure (e.g. Failures: followed by any number of nonzero digits).

My current approach involves removing any line containing "Failures: 0" using:

findstr /v "Failures: 0" consoleText.txt > foo\bar\failures.txt

Then stripping the line above any line containing 'failures: [digits]'. Once I have a file containing these lines structured as follows:

E:\foo\bar\three\tall\cats\testRunOutput\foodbarn\catdog\testoid\ReLogin.html
E:\foo\bar\three\tall\cats\testRunOutput\foobar\topdog\dogstest\Login.html

It should be fairly simple to delete the first 41 characters of every line, Insert the beginning of the url http://build-server:1111/job/foo/bar/testRunOutput/ and finally flip every slash.

I really only need help with

findstr /v "Failures: 0" consoleText.txt > foo\bar\failures.txt

But I thought to post the rest to provide context or get extra code in case someone had gone through this before.

I'm sorry if this is a basic question but I've mostly done command line work on Mac and Linux systems.


Solution

  • you can also use find instead of findstr. find handles spaces like any other character:

    find "Failures: 0"
    

    But what you really want:

    What I need is the lines above any line containging a nonzero failure (eg. 'Failures: ' followed by any number of nonzero digits).

    "Any number of nonzero digits" can be translated with "anything that starts with a nonzero digit"

    findstr /r /c:"Failures:\ [1-9]" 
    

    /r: use Regex (findstr supports a very small subset of Regex)
    \: take the following char (space) literal
    [1-9] any of the digits 1,2,3,...9

    The problem is, the line to be printed is not the line that has to be evaluated. Use a variable (!line!) to keep the last line:

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    for /f "tokens=*" %%a in (t.txt) do (
      echo %%a|findstr /r /c:"Failures:\ [1-9]" >nul  && echo !line!
      set line=%%a
    )
    

    In every iteration of the loop you have !line! = the last processed line and %%a the current line. The trick is, to set line at the very end of the loop.)