The Find and FindStr
utilities both locate files based on their content. The Find utility is
less capable than FindStr, in most cases. The Find utility uses the
following syntax:
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string"
[[drive:][path]filename[...]]
The following list describes each of the command line arguments.
/V
Displays all of the lines that don't contain the specified string.
/C
Displays a count of the lines containing the specified string, but not the actual location.
/N
Displays the line number of each occurrence of the specified string within the file.
/I
Ignores the case of
the characters when searching for the string. Normally, the Find utility
will treat Cat, CAT, and cat as different strings.
/OFF[LINE]
Processes all of the specified files, even if they have the offline attribute set.
string
Defines the text
string that you want to find. The Find utility doesn't allow the use of
regular expressions, so you can only locate strings based on their
actual content.
[drive:][path]
filenameDefines the files to search for the specified string. You can use wildcard characters to define the file specification.
NOTE
If you don't
specify a path, the Find utility searches the text typed at the prompt
or piped from another command. This feature means that you could use
Find to perform tasks such as locating a particular file based on size
in a directory listing (as an example). You can use redirection to let
Find locate particular data in the output of any command or utility.
There's one use for Find
that isn't found in FindStr—you can use Find to determine just the
number of occurrences of the search string in the target file using the /C switch. This particular feature and the smaller size of Find make it useful for scripting tasks where FindStr is overkill.
The easiest way to use
FindStr is to define a simple string and make the call. FindStr looks in
all of the files in the current folder for any words in the string. For
example, typing FindStr "Hello World" *.* at the command prompt and pressing Enter causes the FindStr utility to look for the individual words Hello and World in all files in the current folder. The FindStr utility uses the following syntax:
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[...]]
The following list describes each of the command line arguments.
/B
Matches the search string against the beginning of the line.
NOTE
Some of
the FindStr features are line related. A line of text or other data ends
when FindStr encounters a special carriage return (character number 13)
and line feed (character number 10) combination. When looking at a text
file, you see one line of text separated from another by white space.
The carriage return and line feed combination causes the white space. In
DLLs and other unreadable files, there's no need for lines of text, so
the carriage return and line feed combination appear irregularly, if at
all. This means the lines are much larger and could include the entire
file.
/E
Matches the search string against the end of the line.
/L
Interprets the
search strings literally. This means you no longer have access to
regular expressions, and you also don't have to escape the special
characters.
/R
Defines the
search string as a regular expression. See the "Using Regular
Expressions at the Command Prompt" sidebar for additional information.
/S
Looks for the
search string in the specified files in the current directory and all
subdirectories. When you use this command line switch in the root
directory of a hard drive, you can search the entire hard drive for a
search string. Because FindStr actually looks in every file, you'll find
this process can take a long time.
/I
Performs a case-insensitive search. Normally, FindStr differentiates between Hello and hello—using this command line switch changes that behavior so the two capitalizations are treated the same.
/X
Prints only the lines that match the search string exactly.
/V
Prints only the lines that don't contain an exact match.
/N
Prints the line
number before each line that matches. This option helps you find the
line faster in a text editor that supports line numbering.
/M
Prints only the
filenames of files that contain a match. You can redirect the output
from FindStr to a file to use as an input for a script or other
additional processing.
/O
Prints the
character offset of the matching text in each line. This option helps
you find the text faster in text editors that provide column number
support.
/P
Tells
FindStr to skip files that contain nonprintable characters, such as
executable files. Given that most data files now contain nonprintable
characters, you should probably avoid using this option unless you know
the data appears in pure text files.
/F:
FileReads the list of
files to process from a file. You can also supply a value of "/" to type
the names of the files to check at the command line.
/C:
StringPerforms a literal search with the search string. For example, normally when you type "Hello World" as the search string, FindStr looks for the words separately—lines containing either Hello or World will match. However, when you specify this option, FindStr only matches lines that contain the whole term, Hello World.
This is actually one of the most useful command line switches for
finding just about any information. Let's say you have a series of Word
documents that you're using as help file and that you're looking for the
Accessing Wid-gets header. You could type FindStr /M /C:"Accessing Widgets" *.DOC
and press Enter to locate the required header. FindStr will print out a
list of the files that contain the text you want to locate. However,
let's say that you want to locate any mentions of this topic in the text
instead of seeing just the headers. You could then modify the command
line and type FindStr /M /I /C:"Accessing Widgets" *.DOC and press Enter to perform a case insensitive search.
/G:
FileReads the list of
search strings to look for from a file. You can also supply a value of
"/" to type the search strings at the command line.
/D:
DirectoriesDefines a list of directories to search. You must separate each directory entry with a semicolon.
/A:
ColorAttributeTells FindStr to
display the filenames using colors. You must provide two hexadecimal
(base 16) values from 0 through F (these values are the same as 0
through 15 decimal). The first value is the background color and the
second is the text color. This switch doesn't affect display of the
matching text color, which relies on the current background and
foreground colors of the command prompt.
/OFF[LINE]
Processes all of the specified files, even if they have the offline attribute set.
strings
Defines one or more text strings that you want to find. Use regular expressions (the /R command line switch) to locate text based on wildcards.
[drive:][path]
filename[...]
Defines one or more files to search for the specified string. You can use wildcard characters to define the file specification.
The true power of
FindStr and other advanced text manipulation command line utilities is
that you can create specialized strings using a concept called regular
expressions. A regular expression defines how to look for a string,
rather than precisely which string to find. Regular expressions can
contain the special characters described in the following list.
. (Period)
Provides a placeholder for any single character. For example, "w.ll" could represent the words wall, well, or will.
* (Asterisk)
Represents 0 or more occurrences of the previous character or class. For example, "to*" could represent the words to or too, or simply the letter t.
(Because the asterisk can represent 0 occurrences of a letter, "to*"
can find just the letter t because that's all that's left when you have 0
occurrences of the letter o.)
^ (Circumflex)
Represents a character or class at the beginning of a line. For example, "^Hello" would find the word in the line Hello World, but not in the line George said, Hello.
$ (Dollar sign)
Represents a character or class at the end of the line. For example, "World$" would find the word in the line Hello World, but not in the line World Peace.
[Characters]
Contains a character class (set of characters) from which FindStr selects. For example, "w[ai]ll" will match the words wall and will, but won't match well.
[^Characters]
Contains a character class (set of characters) that FindStr won't select. For example, "w[^ai]ll" will match the word well, but won't match the words wall or will.
[Character-Character]
Specifies a range of characters that FindStr will use for selection. For example, "[a-z]" selects all characters a through z, but not numbers or special symbols.
\Character
Tells FindStr to use
the character literally, rather than as a special character. Programmers
call this process escaping the character. For example, "z\**" locates
terms that begin with z followed by 0 or more asterisks within the file.
\<Characters
Locates the characters when they appear at the beginning of a word. For example, "\<we" locates the words welcome and well, but not the word owe.
Characters\>
Locates the characters when they appear at the end of a word. For example, "we\>" locates the word owe, but not the words welcome and well.
Using
regular expressions lets you create complex search patterns that reduce
search time and ensure good results. For example, you could create a
telephone number search pattern using the search string "(...)...-...."
assuming that your telephone numbers always include an area code and are
formatted using the method shown. I've actually used this search
pattern to locate telephone numbers hidden in DLLs.