Using FINDSTR to search file contents

Posted On 2010-07-12 by dwirch
Tags: Windows Commandline Tutorial Tip Windows Windows 7 Windows Server 2012
Views: 11176

Searching through log files for a particular string of text? How about trying to find a particular piece of source code in a large directory of files? Use FINDSTR to quickly locate the files containing the text you are looking for.

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.


findstr "Uptime Requirement" myfile.txt

In the above example, any lines containing "Uptime Requirement" in the file "myfile.txt" would be printed to the screen.

findstr /s "Uptime Requirement" *.txt

Similar to the first example, the above example would find any lines containing "Uptime Requirement" in any file with a .txt extension, in the current directory and all sub directories.

findstr /x /c:"Uptime Requirement" *.txt

Match .txt files that contain an exact match on "Uptime Requirement". Files that contain "Uptime Requirements" or other non-exact matches will not be displayed. It is important to realize that using /x must be a line that exactly matches "Uptime Requirement". In other words, if anything else is on the same line, it's not an exact match.

findstr /n /i /c:"Uptime Requirement" *

Search for any file containing "Uptime Requirement" regardless of its case and display the line where the text is found.

In the real world ...

One of the things that most people don't know is that I myself have written all the code which runs this site. From time to time, actually about weekly, I do some updates. Most of the back-end of the site is pretty modular, in which most things have been broken down into functions, which can be called from anywhere else on the site.

This being mainly a knowledge base type site, searches are pretty integral. Every time a page is loaded, there are database searches going. One of these searches is encapsulated in a function called FindRelated. What FindRelated does is, given an source article, it parses that article for keywords, tags, etc., then searches the database for articles that might be related to the article that is being viewed by the user.

Now lets say I perform an update to the FindRelated, and I name the new version FindArticle2. Now I've got to go to every page where FindRelated is referenced, and change it to FindRelated2. Did I mention there are about a million files full of code that run this site? Well, maybe not a million, but there are an awful lot of files. Rather than trying to remember which file contains references, I can drop to a command prompt and use FindStr to find all code files within the current directory and all subdirectories that contain references to FindRelated. Here is how I do it:

findstr /s /i /m "FindRelated" *.asp

And that's it. FindStr will happily search all .asp files in the current directory and all its subdirectories for occurences of FindArticle, and print the filenames on screen. Simple.

Command Syntax Reference

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]] strings [[drive:][path]filename[ ...]]

Switch/Value Explanation
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/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 filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename Specifies a file or files to search.

Regular expression quick reference

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

RegEx Explanation
. 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
\ Word position: beginning of word
xyz\> Word position: end of word

About the Author

dwirch has posted a total of 179 articles.

You can find more information from dwirch by visiting

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers