0

I am trying to files in a dir using the command prompt with the line.

findstr /m /i "Pattern1" *.txt | findstr /m /i "Pattern2" *.txt

The command does return results, but only for Pattern2

  • Read help of `Findstr /?` the first findstr will only let pass `filenames` matching Pattern1, the 2nd one ignores the piped input as a file reference is supplied. Better elaborate what you are after. –  Feb 15 '19 at 18:22
  • I am searching for documents matching both pattern1 and pattern2. I see where the problem would exist with piping the findstr with Parameter "/m" into to another "findstr /m" I would like to search without the pipe and use regex both patterns for one findstr. example findstr /m /i "pattern1"and"pattern2" *.txt – Greg LeBlanc Feb 15 '19 at 18:43
  • [What are the undocumented features of FINDSTR](https://stackoverflow.com/q/8844868/62576) – Ken White Feb 15 '19 at 19:02
  • Possible duplicate of [Piping to findstr's input](https://stackoverflow.com/questions/3062100/piping-to-findstrs-input) – krisz Feb 15 '19 at 19:29

1 Answers1

1

To AND the patterns parse the results of the first findstr with a for /f

@Echo off
for /f "delims=" %%A in (
  'findstr /m /i "Pattern1" *.txt 2^>Nul'
) do findstr /mi "Pattern2" %%A 1>Nul 2>&1 && Echo %%A matches both Patterns

If the two patterns appear in an order on the same line, you could have one regex like

findstr /m /i "Pattern1.*Pattern2" *.txt

Otherwise findstr regex capabilities are quite limited.

  • Your first answer is what I was looking for! You are a God among man LotPings! Thank you! – Greg LeBlanc Feb 15 '19 at 19:39
  • 1
    Thanks for feedback, if an answer solves your question or you find it helpful you should consider to [accept the answer](http://stackoverflow.com/help/accepted-answer) and/or [vote up](https://stackoverflow.com/help/why-vote) –  Feb 15 '19 at 20:00