|

How To Use The Findstr Command In Windows (2027)

Findstr searches for text inside files from Command Prompt and prints every line that matches.

The simplest form is findstr "text" filename.txt, and a handful of switches handle case, subfolders and line numbers.

This guide follows Microsoft’s findstr reference, so every switch and example below is documented.

Table of Contents

Findstr quick answers

I want toType this
Find a word in one filefindstr Windows proposal.txt
Find an exact phrasefindstr /c:"hello there" x.y
Ignore upper and lower casefindstr /i windows proposal.txt
Search all subfoldersfindstr /s /i Windows *.*
Show line numbersfindstr /n Windows proposal.txt
List only file namesfindstr /s /i /m computer *.*

The file names here are Microsoft’s samples, so replace them with your own.

Open Command Prompt in the right folder

  1. Press the Windows key and type cmd, then open Command Prompt.
  2. Use cd to move to the folder that holds your files, for example cd C:\Users\YourName\Documents.
  3. Type your findstr command and press Enter.

Findstr searches the current directory unless you give a path.

The findstr syntax

Microsoft gives the syntax as findstr [/b] [/e] [/l | /r] [/s] [/i] [/x] [/v] [/n] [/m] [/o] [/p] [/f:] [/c:] [/g:] [/d:] [/a:] [/off[line]] [:][][ ...].

Microsoft states that all options must come before the search strings and the file names.

Both the search text and at least one file name are required.

Findstr switches at a glance

SwitchWhat it does
/bMatches the pattern only at the beginning of a line.
/eMatches the pattern only at the end of a line.
/lTreats search strings literally.
/rTreats search strings as regular expressions, and this is the default.
/sSearches the current directory and all subdirectories.
/iIgnores the case of the characters.
/xPrints only lines that match exactly.
/vPrints only lines that do not contain a match.
/nPrints the line number of each matching line.
/mPrints only the file name when a file contains a match.
/oPrints the character offset before each matching line.
/pSkips files with non-printable characters.

Switches that take a value

SwitchWhat it does
/c:Uses the text as a literal search string, including spaces.
/g:Gets the search strings from a file.
/f:Gets the list of files to search from a file.
/d:Searches a list of directories separated with semicolons, such as dir1;dir2;dir3.
/a:Sets the color with two hexadecimal digits.
/off[line]Does not skip files that have the offline attribute set.

Search for a phrase, not separate words

Microsoft says spaces separate multiple search strings unless the argument is prefixed with /c.

So findstr hello there x.y looks for either word, while findstr /c:"hello there" x.y looks for the whole phrase.

Search every file in a folder and its subfolders

Add /s to search the current directory and every folder below it.

Microsoft’s example findstr /s /i Windows *.* searches every file for Windows and ignores case.

Add /m when you only want to know which files contain the text.

Regular expression characters

CharacterMeaning
.Any one character.
*Zero or more occurrences of the previous character or class.
^The beginning of the line.
$The end of the line.
[class]Any one character in the set.
[^class]Any one character not in the set.
[x-y]Any character in the range.
\xA literal use of a metacharacter.
\The beginning of a word.
string\>The end of a word.

Microsoft notes that the special characters work best in combination, and .* matches any string of characters.

Match whole words

Microsoft's example findstr /s /i /m \ *.* lists every file that contains the word computer, whatever its case.

Changing it to \ also matches words that begin with comp, such as compliment and compete.

Use a file of search terms

To search for several strings, put each one on its own line in a text file.

Then run Microsoft's example findstr /g:stringlist.txt /f:filelist.txt > results.out to read the terms from one file, the files from another, and save the output to results.out.

Find lines that start with a word

Microsoft shows findstr /b /n /r /c:^ *FOR *.bas for lines that begin with FOR, optionally after spaces, with their line numbers.

Literal text or regular expression

Findstr treats your search text as a regular expression by default, so a dot matches any character.

Add /l to search for the characters exactly as typed, which is safer when your text contains symbols.

Add /r only when you want pattern matching, since it is already the default.

Show lines that do not match

Use /v to print only the lines that do not contain a match.

Use /x to print only lines that match the search text exactly.

Use /b or /e to anchor the match to the start or end of a line.

Search several folders at once

The /d switch takes a list of directories separated with semicolons.

For example, findstr /d:C:\Logs;C:\Reports error *.txt would search the two folders for the word error, and the folder names here are placeholders.

Mistakes that break a findstr command

  • Putting a switch after the file name, because Microsoft says all options must precede the strings and file names.
  • Searching a phrase without /c:, so each word is treated as a separate search string.
  • Leaving out the file name, since at least one is required.
  • Forgetting /s and wondering why files in subfolders are missed.
  • Using . in a pattern when you meant a literal period, which needs \. or the /l switch.

Save the results to a file

Add > results.txt to the end of any findstr command to write the matching lines to a file instead of the screen, as Microsoft's > results.out example does.

This helps when a search across many folders prints more lines than the window can show.

Combine it with /m to save just the list of file names that contain your text.

  1. Run the search on screen first and confirm the matches look right.
  2. Press the Up arrow to recall the command.
  3. Add > results.txt and press Enter.
  4. Open results.txt from the same folder to read the output.

The PowerShell alternative

PowerShell has the Select-String cmdlet, which Microsoft documents as matching case-insensitively by default.

NeedSelect-String parameter
Case-sensitive match-CaseSensitive
Literal text, not a regular expression-SimpleMatch
Lines that do not match-NotMatch
Lines around each match-Context 2,3
Only the first match per file-List

A basic example is Select-String -Path .\*.txt -Pattern 'Get-', which searches every .txt file in the current folder.

Related guides

For network searches see the Netstat guide, which pairs well with a findstr filter.

To copy the files you find, see the Robocopy guide, and for system file checks see how to run SFC and DISM.

Official sources

Every switch, example and metacharacter comes from Microsoft's findstr reference.

The PowerShell details come from Microsoft's Select-String reference.

Leave a Reply

Your email address will not be published. Required fields are marked *