findstr review: Handy Windows tool for command searches for text strings
findstr is a built-in tool of the Windows operating system that you may run from the command line to find text in files or in command-line outputs. In other words, if you want to find specific exact match text strings in files, in a command-line output, or elsewhere, you may use the free findstr command on Windows to do so. It is available in all major Windows versions including Windows 10.
You can use the application to filter command-line outputs and search individual files or entire directory structures for files with matching text.
Run findstr /? from the command line to display all parameters and options that "Find String" supports.
Third-party tools like Notepad++, GGRep, and Everything support finding text in files as well.
What is findstr – Search for Text in Windows Files
findstr is a powerful command that you may use to search for strings in files or to filter command line output. You may use it to scan entire directory structures or drives for files that match the selected string or part of it, and to find specific text in command line outputs quickly.
You can run findstr from the command line or as a batch file. Open a new command line prompt by clicking on the Windows-key, typing cmd.exe, and selecting the result. Alternatively, use the Run command to open findstr.
Some Useful Parameters in findstr
As you explore findstr, the following parameters will help you find specific texts. For example, you can look for exact match or case-sensitive words inside files.
- /? -- display the help text
- /S -- searches the directory and all subdirectories
- /I -- search is not case sensitive
- /R -- use search strings as regular expressions
- /B -- matches patterns at the beginning of lines
- /P -- skip files with non-printable characters
- /V -- print only lines that contain a match
- /N -- print the line number
findstr Examples That You May Find Useful
Consider these examples to try with findstr:
- ipconfig | findstr "192.168" -- The command runs ipconfig and returns any result that matches 192.168. Any other result is ignored.
- netstat | findstr "123.123.123.13" -- Runs the netstat command and returns any result that matches the string (in this case the IP address).
- findstr /c:"windows 10" windows.txt -- Searches the document windows.txt for the string "windows 10"
- findstr "windows 10" windows txt -- Searches for "windows" or "10" in the file.
- findstr "windows" c:\documents\*.* -- Searches any file under c:\documents for the string "windows".
- findstr /s /i Windows *.* -- Searches every file in the current directory and all subdirectories for the word Windows ignoring letter case.
- findstr /b /n /r /c:"^ *FOR" *.bas-- Returns any line that begins with FOR that are preceded by zero or more spaces. Prints the line number as well.
Advanced options include returning content that is found at the beginning or end of lines, using regular expressions, or using wildcards.
Closing words
findstr's main advantage is that it is a built-in tool that you can run on any Windows machine. It is useful to find text in files quickly but works as a tool to filter the output of command-line tools as well. It’s free, quick, and easy to use – what more do you need from a search tool?
Now you: Which program do you use to find text in files?
I want to look for word XX in all files with names containing YY. How to do that?
For reference, here is how:
findstr /s /i /M XX C:\Data\*YY*.*
If the seach string or file spec have space characters (or other that will interfere with the command line logic), use :
findstr /s /i /M /C:”XX ZZZ” “C:\Data\*YY*.*”
This will display a list the paths\filenames of all *YY*.* files found in specified directory subtree C:\Data\ IN WHICH the search string is found –
Each file will show up only once, even if some files contain the string multiple times
/s Specifies a recursive search in specified directory (or current one if not specified)
i.e. searches directory AND all subdirectories for all matching files
/i Specifies a CASE-INSENSITIVE search
/M Only prints the filename if a file contains a match
Example result output :
C:\Data\Download\CHROME_EXTENSIONS_01.txt
C:\Data\Archive\OLD\CHROME_EXTENSIONS_OLD.txt
For more details, see reply to Alexd below.
There is also this neat little tool called FART-IT [https://github.com/lionello/fart-it], a Find And Replace Text command line utility, which runs flawlessly in my experience.
Hi, thanks for the tip. Looks great.
Easy-to-remember name, too.
As is yours ! ;o)
We sound differently though, I hope :)
This is useless if the purpose is to locate a certain string where you don’t know where it is in a file in subdirectory.. if I knew the file, I just load into my favorite text editor.
So your title is misleading “to find text in files and command outputs” –
Hi Everyone
This can be useful for some of you.
dir /s /b | findstr “word” – searches for string word in each line anywhere within the line
dir /s /b | findstr “^word” – searches for string word in each line and returns result only if the line starts with this string
dir /s /b | findstr “word$” – searches for string word in each line and return result only if line finishes with this string
Good luck
I use the findstr /n “aaaa” “mypath”
to extract number of line where mystring is found.
But if i have:
aaaa
aaaa
aaaa
in c:\mypath\t.txt the code return 1 aaa, 2 aaa, 3 aaa
for my use i need only the number of first occurrence, in my case 1 aaa
How to?
Hi i need to find this line in a file:” ”
There is 2Tab and some ” in the line…
Is it possible to somehow sertch for the line with ” in it, without make it a new string
This is the output i get…
X:\as\tomcat-XXXXX\webapps\XXXXX\WEB-INF\classes>findstr /C:”” XXXXX-job.xml
FINDSTR: Cannot open 0
FINDSTR: Cannot open 3
FINDSTR: Cannot open ? />
TASKLIST>>lista1
findstr /s /i /n /p /c:”123.exe” lista1>>result
lista1:117:123.exe 2392 Console 1 33.328 k
Preciso encerrar a task 123.exe varias vezes, e quero facilitar com um arquivo.BAT.
Gostaria de completar este BAT onde obtivesse o valor do PID (2392) desta task,
á ser encerrada, já que ele muda toda vez que 123.exe reinicia. (Após as 2 linhas acima)
obtive o arquivo result (3a linha).
Como se faz para usar este novo PID no final do BAT ? ex:
“taskkill /PID 2392”
When you search for texts in multiple (log) files, then maybe this helps:
$path = “C:\Temp\logFiles”
$searchString = “Critical Error”
Get-ChildItem -Path $path -Filter “*.log” | Select-String -Pattern $searchString | select Filename, Line
The filter is not really needed in this particular example, but might be useful in other cases.
Sources:
https://stackoverflow.com/questions/8153750/how-to-search-a-string-in-multiple-files-and-return-the-names-of-files-in-powers#8153857
https://stackoverflow.com/questions/17658026/get-parent-folder-file-name-from-a-select-string-output
Nirsoft’s SearchMyFiles is able to search the content of text-based files (eg. TXT, XML, INI, CFG, etc.)
findstr can also be used in combination with DISM. For example:
dism /online /get-packages | findstr 2976978
If KB2976978 which is Microsoft’s perrenial telemetry package for 8.1 was installed it would result in:
Package Identity : Package_for_KB2976978~31bf3856ad364e35~amd64~~6.3.1.0
To remove via a command prompt: wusa /uninstall /kb:2976978 /quiet /norestart
Actually, come to think of it, the remove command should be:
DISM.exe /Online /Remove-Package /PackageName:Package_for_KB2976978~31bf3856ad364e35~amd64~~6.3.1.0 /quiet /norestart
The actual version number, 6.3.1.0 in this particular example, may vary since M$ is still trying to persuade 8.1 users to install this crap and it reappeared last month I noticed.
For my use cases I use often findstr on results piped from ‘strings’ by sysinternals. It’s not the most powerful tool, but it’s just fine for quick jobs.
Thank you Martin. Good tutorial that helps people to find some simple but very useful tools.
PS: Thanks Jessica. I always forget about that Powershell has the same tools as cmd (and much more), and often more powerful.
I use grepWin 1.7.2.730 x64 Portable when I search something inside my batch files:
https://tools.stefankueng.com/
I use FINDSTR all the time (and sometimes PowerShell).
Note that FINDSTR does’t support output of all command line tools. For example the output of REG.exe (UCS2-LE encoded) and possibly all unicode files can not be read.
Workaround is: TYPE file | findstr parms or using oldschool FIND command.
BTW: Someone did some *really* serious FINDSTR research and found undocumented features and bugs. That can be found here: https://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman
Warning: long read.
I really like Everything, but mostly for finding files, not content.
Filenames are indexed; content is not (luckily!), so that might take a little longer compared to the ‘normal’ lightspeed of Everything.
Great information, thanks a lot Maarten!
PowerShell equivalent is
Select-String
.https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string
Thank you Jessica!
Agent Ransack is the best.
You mean FileLocator Lite?
Been using that for years.
Still findstr might have its uses.
I like the Cortana suggestion above by lazyfingers. Always kind of ignored it but for looking up some administrative functions. I do see the documents section and it works like a charm, though I do like FileLocator Lite better.
Thanks for the article, Martin. I didn’t know the command existed.
Hate to say it but I use Win10 search engine in Cortana. It has icon filters for documents, photos, music, etc. ex. you want to find ‘apples’ in a doc you have but can’t recall which one (and the file name doesn’t have ‘apples’ in it). Just type apples in the search box, click documents icon, and there it is. Everything app may find text too but it seemed more complicated.
What’s your secret? I can never get Windows 10 search to find anything.