Findstr Command

This command is supported by Windows NT family operating systems. This command isn't supported by Windows 9x or MS-DOS systems. This command is quite useful when you want to find specific text in files. This command utility is quite like the UNIX "grep" command. So I included its command reference here for you to read. Of course, you can refer to Windows NT Command-line Command Help or Windows 2000/XP Online Help for information about this command.

Although it is not often used, it is quite helpful when you don't want to download additional software to find text strings in files. Since Windows 2000, Indexing Service provides a much more powerful way for searching files.


Toggle font size

Findstr

Searches for strings in files using literal text or regular expressions. Click Devinfo Notes in the Related Topics list for a list of the regular expression symbols accepted by findstr.

findstr [/b] [/e] [/l] [/c:string] [/r] [/s] [/i] [/x] [/v] [/n] [/m] [/o] [/g:file] [/f:file] [/d:dirlist] [/a:color attribute] [strings] [[drive:][path] filename [...]]

Parameters

/b

Matches the pattern if at the beginning of a line.

/e

Matches the pattern if at the end of a line.

/l

Uses search strings literally.

/c: string

Uses specified text as a literal search string.

/r

Uses search strings as regular expressions. This switch is not required; findstr interprets all metacharacters as regular expressions unless the /l switch is used.

/s

Searches for matching files in the current directory and all subdirectories.

/i

Specifies that the search is not to be case sensitive.

/x

Prints lines that match exactly.

/v

Prints only lines that do not contain a match.

/n

Prints the line number before each line that matches.

/m

Prints only the file name if a file contains a match.

/o

Prints seek offset before each matching line.

/g file

Gets search strings from the specified file.

/f file

Reads file list from the specified file.

/d dirlist

Searches a comma-delimited list of directories.

/a color attribute

Specifies color attributes with two hexadecimal digits.

Use spaces to separate multiple search strings unless the argument is prefixed with /c, as shown in the following example:

findstr "hello there" x.y

searches for "hello" or "there" in file x.y. However, the following command searches for "hello there" in file x.y.

findstr /c:"hello there" x.y


Findstr is capable of finding the exact text you are looking for in any ASCII file or files. However, sometimes you have only part of the information that you want to match, or you want to find a wider range of information. In such cases, findstr has the powerful capability to search for patterns of text using regular expressions.

Regular expressions are a notation for specifying patterns of text, as opposed to exact strings of characters. The notation uses literal characters and metacharacters. Every character that does not have special meaning in the regular expression syntax is a literal character and matches an occurrence of that character. For example, letters and numbers are literal characters. A metacharacter is a symbol with special meaning (an operator or delimiter) in the regular-expression syntax. These are the metacharacters accepted by findstr:

. Wildcard: any character
* Repeat: zero or more occurrences of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word

The special characters in regular expression syntax are most powerful when they are used together. For example, the following combination of the wildcard character (.) and repeat (*) character

.*

matches any string of characters. This expression is useful when it is part of a larger expression, such as

b.*ing

which matches any string beginning with "B" and ending with "ing".


To find all occurrences of the word "Windows" (with an initial capital W) in the file Proposal.txt, type

findstr Windows proposal.txt

To search every file in the current directory and all subdirectories that contained the word Windows, regardless of the letter case, type

findstr /s /i Windows *.*

To find all occurrences of lines that contain the word "FOR", preceded by any number of spaces, (as in a computer program loop), and to include the line number where each occurrence is found, type:

findstr /b /n /c:" *FOR" *.bas

If you want to search for several different items in the same set of files, create a text file that contains each search criterion on a new line. You can also list the exact files you want to search in a text file. To use the search criteria in the file Finddata.txt, search the files listed in Filelist.txt, and then store the results in the file Results.out, type:

findstr /g:finddata.txt /f:filelist.txt > results.out

Assume you wanted to find every file in the current directory and all subdirectories that contained the word computer, regardless of the letter case. To list every file containing the word computer, type

findstr /s /i /m "\<computer\>" *.*

Now assume you want to find not only the word "computer," but also any other words that begin with the letters comp, such as "compliment" and "compete. " Type:

findstr /s /i /m "\<comp.*" *.*

^ Top of page

Return to Overview