Using FINDSTR to search file contents

By | 2010-07-12

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.

Examples

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/ValueExplanation
/BMatches pattern if at the beginning of a line.
/EMatches pattern if at the end of a line.
/LUses search strings literally.
/RUses search strings as regular expressions.
/SSearches for matching files in the current directory and all subdirectories.
/ISpecifies that the search is not to be case-sensitive.
/XPrints lines that match exactly.
/VPrints only lines that do not contain a match.
/NPrints the line number before each line that matches.
/MPrints only the filename if a file contains a match.
/OPrints character offset before each matching line.
/PSkip files with non-printable characters.
/OFF[LINE]Do not skip files with offline attribute set.
/A:attrSpecifies color attribute with two hex digits. See “color /?”
/F:fileReads file list from the specified file(/ stands for console).
/C:stringUses specified string as a literal search string.
/G:fileGets search strings from the specified file(/ stands for console).
/D:dirSearch a semicolon delimited list of directories
stringsText to be searched for.
[drive:][path]filenameSpecifies 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.

RegExExplanation
.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
\xEscape: literal use of metacharacter x
\Word position: beginning of word
xyz\>Word position: end of word
Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.