If you want to find specific text in files, in a command line output or elsewhere, you may use the findstr command on Windows to do so.
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.
You can use the application to filter command line outputs, 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, or Everything support finding text in files as well.
You can run findstr from the command line or batch files. Open a new command line prompt by tapping on the Windows-key, typing cmd.exe and selecting the result.
Useful parameters:
Here is a list of examples that you may find useful:
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 specified text in command line outputs quickly.
Advanced options include returning content that is found at the beginning or end of lines, using regular expressions, or using wildcards.
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.
Now you: which program do you use to find text in files?
Please click on the following link to open the newsletter signup page: Ghacks Newsletter Sign up
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.
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.
Thanks for the article, Martin. I didn’t know the command existed.
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.
PowerShell equivalent is
Select-String
.https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string
Thank you Jessica!
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!
I use grepWin 1.7.2.730 x64 Portable when I search something inside my batch files:
https://tools.stefankueng.com/
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.
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.
Nirsoft’s SearchMyFiles is able to search the content of text-based files (eg. TXT, XML, INI, CFG, etc.)
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
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”
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 ? />
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 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
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” –
I want to look for word XX in all files with names containing YY. How to do that?