Use findstr on Windows to find text in files and command outputs - gHacks Tech News

Use findstr on Windows to find text in files and command outputs

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.

Using findstr

findstr

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:

  • /? -- 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

Here is a list of examples that you may find useful:

  • 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.

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.

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.

Now you: which program do you use to find text in files?

Summary
Use findstr on Windows to find text in files and command outputs
Article Name
Use findstr on Windows to find text in files and command outputs
Description
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.
Author
Publisher
Ghacks Technology News
Logo

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:

Comments

  1. lazyfingers said on May 4, 2018 at 3:10 pm
    Reply

    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.

    1. John Fenderson said on May 5, 2018 at 12:15 am
      Reply

      What’s your secret? I can never get Windows 10 search to find anything.

  2. Mark Hazard said on May 4, 2018 at 4:02 pm
    Reply

    Thanks for the article, Martin. I didn’t know the command existed.

  3. Anonymous said on May 4, 2018 at 4:47 pm
    Reply

    Agent Ransack is the best.

    1. BM said on May 5, 2018 at 7:25 am
      Reply

      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.

  4. Jessica said on May 4, 2018 at 10:27 pm
    Reply
    1. Martin Brinkmann said on May 5, 2018 at 7:13 am
      Reply

      Thank you Jessica!

  5. Maarten said on May 5, 2018 at 2:36 am
    Reply

    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.

    1. Martin Brinkmann said on May 5, 2018 at 7:29 am
      Reply

      Great information, thanks a lot Maarten!

  6. Robert G. said on May 5, 2018 at 3:29 am
    Reply

    I use grepWin 1.7.2.730 x64 Portable when I search something inside my batch files:

    https://tools.stefankueng.com/

  7. STech said on May 7, 2018 at 8:37 pm
    Reply

    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.

  8. TelV said on May 8, 2018 at 11:45 am
    Reply

    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

    1. TelV said on May 8, 2018 at 12:00 pm
      Reply

      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.

  9. Anonymous said on May 8, 2018 at 10:01 pm
    Reply

    Nirsoft’s SearchMyFiles is able to search the content of text-based files (eg. TXT, XML, INI, CFG, etc.)

  10. carlos said on May 19, 2018 at 9:26 am
    Reply

    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

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.