≡ Menu

Findstr command examples and regular expressions

Findstr command on Windows is useful for searching for specific text pattern in files. It’s functionality is similar to the grep command on Linux OS. You can find below the syntax of ‘findstr’ for various use cases.

Search for text/string in a file:

findstr pattern  filename

For example, to search for the string ‘Windows’ in the text file  CLItips.txt, the command would be as below.

findstr Windows CLItips.txt

Note that the above command looks for exactly ‘Windows’. It considers case by default. So if you have a line that has the word ‘windows’, it would not be printed in the output of the above command.

Ignore text case:

You can add /I switch to ignore the case in the search. So if you run ‘findstr windows /I CLItips.txt‘, it does case insensitive pattern matching.

Search for any of the given words

If you want to print the lines having any of the given word set, you can enclose the list of words in double quotes in findstr command.

findstr "word1 word2 word3.."  filename.txt
findstr "Apple Orange"  fruits.txt

This command would print a line if it has  has either the word ‘Apple’ or the word ‘Orange’ or both the words.

Search for pattern with multiple words

findstr /C:"word1 word2 word3..."  filename

/C indicates that the search pattern has to be matched literally.

For example, to search for the string “Apple Ball Cat” in file Book.txt, the command would be as below

 findstr /C:"Apple Ball Cat" Book.txt

Search with Regular Expressions

You can use regular expressions with findstr /R switch. Typical command would be as below.

findstr /R pattern  filename.txt

Here the pattern can be specified using regular expressions.

Search for the occurrence of all words ending with ‘xyz’ in a file.

findstr /R [a-z]*xyz  filename.txt

Search for text in all the files in a current directory

You can use wildcard ‘*” to specify that all the files in a directory should be searched for the given string.
For example, to search for ‘windows’ in all the files in the current directory, you can use the below command.

findstr /I  windows *

To search all the text files in the directory C:\data:

findstr /I windows C:\data\*.txt

Search for multiple strings

If you need to search for multiple strings, then you can do that with the below batch script.

@echo off

for /F %%i in (pattern.txt) do (
echo Files containing %%i
findstr /M /C:%%i /S *.txt

‘pattern.txt ‘is the file having the strings(one per line) that need to be searched for. The above command searches only text files. You can customize the findstr command in the script to search in files with other extensions.  Adding ‘/M’ option to the command causes to print only the file names.

Print only the lines where the given string is at the beginning of the line.

You can add /B switch to indicate that the specified string should be in the beginning of the line.

findstr /B /C:windows CLItips.txt

Print only the lines where the given string is at the end of the line

findstr /E /C:windows CLItips.txt

Print line numbers for all the matched lines.

You can add /N switch to the findstr command to print line numbers for the matched lines.

Print only the filenames

finstr /M /C:"pattern"  *.log

This command searches for the pattern in all *.log files, and then prints only the file names having the pattern. Even if a file has multiple occurrences of the pattern, it’s printed only once by findstr.

11 comments… add one
  • Khoi

    How do I use either the FIND or FINDSTR commands to return a list of ASCII files containing a string? For example, I have 2000 ASCII files (.txt) in one or several directories (c:\temp, c:\temp1) and I need the path and file name of files that contain the string “USA”? Thanks.

    • admin

      You can use /S for searching files in all the subdirectories recursively. For your example the command would be as below:
      findstr /S /C:"USA" *
      The above command would print the matched lines also. If you want only the file names to be printed then the command would be:
      findstr /S /M /C:"USA" *

    • admin

      And if you want to search multiple directories then the command would be:

      findstr /S /M /C:"USA" /D:directory1;directory2;directory3 *

      If you want to search only text files then use the below command(Above command would match any file type like .xls, .pdf, .doc etc.)

      findstr /S /M /C:"USA" /D:directory1;directory2;directory3 *.txt

  • Murray

    Hi, great info, thanks.
    TYPO—–This does not seem to run.

    findstr /S /M /I /C:"murray salmon" /F: *

    I want to search all folders on C and F drives for ANY document type that has the words murray and salmon.


  • Eszter

    Does anybody know how I can display one more line after the matching pattern?
    (In Linux, I can do this with the ‘grep -A1 “pattern” filename’ command)

  • Steffen

    I need to find the string “Milwaukee” in several *.txt files and then move the files to another directory.
    Does anybody know how to do that?
    Thank you

  • Bill Clinton

    another source of bugs from microsoft

  • Abhishek

    I want to search specific word from text file could be present at any location on my system. I am writing this please tell what is wrong it is not working.

    @echo off
    FINDSTR /C:“up to date” C:\Users\G510840\Desktop\dbstatus.txt
    if %errorlevel%==0 
    %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState
    SLEEP -m 100
    goto START 
  • bharat

    I want to know, how to search and output only the nth occurrence of pattern matched, using findstr.

  • Samba999

    How do you delete any found files? Thanks :)

  • 1Roso

    To search for multiple strings you can simply do for literal strings:

    findstr /c:”one” /c:”two” /c:”three” filename.txt
    findstr “one two three” filename.txt

    as by default space is the delimiter ;)

Leave a Comment