0

I want to do following in a bat file

  1. Run a exe
  2. Capture results from step 1
  3. find a string in results from step 2
  4. if step 3 find is successful then do something

Is this possible(particularly worried about the find part)? Any help is appreciated. Thanks.

tugga
  • 113
  • 4

2 Answers2

1
yourcommand | findstr "blah" > nul 2>&1
if not errorlevel 1 (
    :: do something
) else (
    echo Failed!
)
user1686
  • 10,162
  • 1
  • 26
  • 42
  • Thank you for the answer. It is perfect. Sorry for my ignorance, but I didn't understand the > 'nul 2>&1' part. Can you please point me to some link or explain? I can't upvote bcoz of my reputation. – tugga Nov 30 '09 at 11:59
  • 1
    The "> nul" part redirects standard output to nul (like the /dev/null device on a *nix system), and the "2>&1" part redirects any errors to null as well. – RainyRat Nov 30 '09 at 13:36
  • If what you need to do is pretty short then you can also put it into the same line: `program | findstr foo >nul 2>&1 && do something`. – Joey Nov 30 '09 at 19:33
-1

I don't know if it's possible with just the standard Server '03 tools, but you can certainly do something like that with the freely-available UnxUtils. A script to do what you need would go through the following steps:

1. TheThingYouWantToRun.exe > C:\tmp\output.txt
2. if wc -l `grep [string] C:\tmp\output.txt` > 0 then [do something]
3. else end.
RainyRat
  • 3,730
  • 1
  • 24
  • 29
  • 2
    Ouch, first of all, heaping loads of UNIX utilities onto the system isn't necessary in this case. Second, using `wc` here is overkill, as `grep` already yields a return code that tells you whether a line has been found or not. Third, the temporary file is unneeded (and shouldn't be written to a directory where by default no normal user has access) as you can simply pipe the program's output into grep. It then reduces to `program.exe | grep foo && echo line found || echo line not found`. And that looks exactly the same when using `findstr` instead of `grep`. – Joey Nov 30 '09 at 19:30