Search code examples
windowscmdpipefindstr

Trying to find files with FINDSTR using pattern1 and pattern2 in Windows Command prompt


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


Solution

  • 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.