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.
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.
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" *
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
Hi, great info, thanks.
TYPO—–This does not seem to run.
I want to search all folders on C and F drives for ANY document type that has the words murray and salmon.
You are searching for the phrase “murray salmon”
Remove /C: and then it would search for murray or salmon – this way you will get at least some results and if desired you can filler out the output of that.
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)
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?
You can search for file names that contain your desired string and copy them through a single command. Please check the code below
rem — move to the directory you want to do the search, let us say d:\myfolder
for /f %%i in (‘findstr /s /I /M /C:”Milwaukee” “*.txt”‘) do copy %%i d:\OtherDirectory\%%nxi
rem — job done, move to original folder
another source of bugs from microsoft
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.
I want to know, how to search and output only the nth occurrence of pattern matched, using findstr.
How do you delete any found files? Thanks :)
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 ;)
I want to to extract only “file_size=8452858 rows_count=29339” from the below line.
Actually i have many such files which has multiple entries like below but multimple entries have the same value in a single file.
Basically i want to extract string matching file_size and then say 40 characters including including file_size.
file_arrival_date=2017-03-29 file_size=8452858 rows_count=29339 file_type=dat file_source_system
can some please help.
I want to search a specific word in a lot of archives, in a lot of subdirectories.
FINDSTR “word” *
But in this way, I can search only in the directory, not in all subdirectories.
Use /S parameter before your word.
CAN I ANY ONE HELP ME TO REMOVE EXPRESSION SEPARATOR FROM TEXT FILE. IN CMD LINE.
EXAMPLE: CONVERT FROM THIS “127,231,653” TO THIS 127231653
@Atr first do this so you can learn:
then use /S like:
FINDSTR /S “word” *
I have the following command to list the last 9 entries of a directory. The entries are numbered. How can I extend that range to over 9 entries? When I specify a range such as [1-10], weird results are shown.
dir /b /p /w /od|findstr/n ^^|findstr “^[1-9]:”
My operating system is Windows 10 Home.
Add quotes around pattern and filename and add full directory to filename for accuracy
How can i extract the words with in two different strings and put them site by site.?
C:\>some user some word
Can we use the findstr command to search a particular string/pattern in batch file too? I mean say if there is a batch file with name a.bat and it has got the string program in that batch file.
Now if i use the findstr /I “program” * commnad, will that give a.bat file also?.I want to know yes or no.
And i tried doing that and it is giving the a.bat file but one more doubt for me,while executing this findstr command on batch likes the above, will that findstr internally executes all the commands in that bathc file?just wannna know.
This is out of date.
> findstr “—– FAILED” output.txt
FINDSTR: /- ignored
FINDSTR: /- ignored
FINDSTR: /- ignored
FINDSTR: /- ignored
FINDSTR: / ignored
FINDSTR: /F ignored
FINDSTR: /A ignored
FINDSTR: /D ignored
I’m having the line “case IF-1234 this is a text file”
In my above string I want to print IF-1234. can some one help me how i can achieve this using one single windows command.
Please help with command(Findstr) for Copying the Line Containes in the input.txt from MasterBasedata.txt and to be writing in new file Output.txt
Ex. In input file i’ve the srtrins to be searched, Ex Multipal Mobile Number, In MasterBasedata.txt I’ve the detail of Mobile numbers i’m searching for, I need to write the line which is having matching mobile numbers in new file output.txt.
I want to capture the 2nd word( i.e. the date) from the 2nd line from the below output of a command(let it be xyz)
Data source CODA enabled.
=== 03/13/19 01:45:00 PM
Instance : 0
GBL_COLLECTOR : Nums 12.02.008
GBL_INTERVAL : 299
I am using the below, but getting error,
For /f “tokens=2” %a in (‘xyz | findstr == ‘) do echo %a
Can anyone help me find the exact syntax to capture the above ?
In this case, you have to include tokens 2 and 3, and disregard token 1, also suppose 1.txt has your log content:
for /f “tokens=1,2,3 usebackq” %a in (`type 1.txt ^|findstr “==”`) do echo %b %c
echo 03/13/19 01:45:00
I use a findstr in a txt file, but in txt have a similar line, for example:
the findstr return all lines, i need only the first line numer of match, not all.
What command are you using to extract all lines? I am going getting the last line and I need all lines.
How do I find rows in a file having only commas using findstr and print other lines.
For eg in below 3 lines I wnat to print only row 1,3 to a new file
I have couple of lines in a file Test.txt like
abc = ‘123’
abcd_dc = ckdhuadh
I want to search exact string “abc = ”
I used below command
echo %abc%| FINDSTR /B “abc =” Test.txt
But it is taking both lines. Need help
I have about 150’000 PDF files in many directories. a lot of them have an author of a certain value. I only need the PDF’s that do not have this author
can I do this with findstr ?
How to find lines with space characters? None of the following work:
echo “ab cd” | findstr ” ”
echo “ab cd” | findstr /R “[ ]”
echo “ab cd” | findstr C:” ”
echo “ab cd” | findstr /R C:”[ ]”
Nor many other variations I have tried using ’s and /R’s or even “\ ” seem to work.
Interestingly: findstr C:”b c” and findstr C:” c” print “ab cd” but findstr C:”b ” prints nothing.
Your missed a slash in your third example:
C:\>echo “ab cd” | findstr /c:” ”
i remeber have created command dos that simulate the grep command
it’s sound like this
for %m in (*.*) do findstr “stringtofind” %m
but i cant make it now,
who can help
Hi – I want to search two strings in all sub directories text files. And want to save yes/no value (depending on the availability of these strings) in a boolean variable using windows batch command. This will help me to make my jenkins build stable/unstable.
Thanks in advance!
I’m writing a script where I want to search for particular pattern present in a file, so the result which I’m looking for is, should start with a ‘space’ or a ‘(‘ and ends with a semicolon. could you pls help me?
this is not working
findstr /S /I /X /N “include” *.c*
i want to compare file name. for eg. if file contains today’s date then do not delete it.
How to use findstr for this. is there any other function ?
Is there a way to do this grep command with “Windows” FINDSTR
how to use findstr in .xlsx file ?
in this data:
a|b|c|d| :: how to get the value of ‘d’ using any of the windows tools when piped