Findstr command examples regular expressions

by admin on June 1, 2011

Findstr command on Windows is useful for searching for specific text in files. This command is Windows counterpart for the Linux command grep. You can find below the syntax of this command for various use cases.

Search for text/string in a file:

findstr /C:string  filename

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

findstr /C: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 /C:windows /I CLItips.txt‘, it does case insensitive pattern matching.

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  /C:windows *

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

findstr /I /C: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.

{ 3 comments… read them below or add one }

Khoi August 30, 2011 at 5:04 pm

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.

Reply

admin August 30, 2011 at 5:56 pm

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 August 30, 2011 at 6:10 pm

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

Leave a Comment

HTML tags are not allowed.

Previous post:

Next post: