Previous section   Next section

SEARCH COMMANDS

Windows Server 2003 provides commands for locating files and folders as well as a specific string of text within files. This section covers some of them.

find Command

The find command searches for a specified string of text in one or more files and displays any matches. The syntax is

find [options] "string" filename(s)

The "string" parameter is required and consists of the string of text that you wish to match. As in the above syntax example, the string must be enclosed in quotes and must be found in files in its entirety. It cannot be interrupted with a carriage return.

The filename(s) parameter is the name of one or more files to be searched, with the drive designation and path name included if necessary. Multiple file names should be separated by a space, and cannot be designated with wildcard characters. In place of a file name, the find command can accept input from the keyboard, from another command via a pipe, or from a file via a redirection command (<).

The options available for the find command are defined in Table A.68.

Table A.68. find Command Options

Option

Description

/c

Outputs a count of the lines that match the string instead of the lines themselves. This option should not be used in conjunction with the /n option.

/i

Ignores differences between uppercase and lowercase when trying to match the string.

/n

Includes the line number of the matched line when displaying it.

/v

Displays the lines that do not match the string. If used in conjunction with the /c option, displays the number of lines that did not match the string.

FIND COMMAND EXAMPLES

Enter the following to find all files on a disk that contain the word "auto":

dir c:\ /s | find /i "auto"

findstr Command

The findstr command is a more comprehensive search mechanism than find; it will match a string of text exactly or use regular expressions to match a pattern of text. Regular expressions use both metacharacters and actual characters to define the search pattern. Table A.69 describes acceptable metacharacters, which can be used singly or in combination.

The syntax for findstr is

findstr [options] strings file(s)

The strings parameter contains the text the command is searching for in the file(s) listed. If there is more than one string of text, they must be separated with spaces. If the string itself has spaces, the /c:string option must be used as described in Table A.70.

Table A.69. Metacharacters

Character

Description

. (period)

Wildcard. Will match any character.

*

Repeat. Will find zero or more occurrences of the previous character or class.

^

Beginning of the line.

$

End of the line.

[class]

Matches any one character (class) enclosed in the brackets. For example, [abc] will match one of the characters a, b, or c.

[^class]

Matches any character (class) not enclosed in the brackets. For example, [^abc] will match any character except a, b, or c.

[range]

Will match any character within the range, where range is in the format of xy. To match any character in the range mp, enter [m-p].

\x

Represents the metacharacter, x, literally. For example, to match $, enter \$.

\<xyz

Matches xyz at the beginning of a word.

xyz\>

Matches xyz at the end of a word.

\<xyz\>

Finds anything containing the letters xyz.

.*Matches any string of characters.

Table A.70. findstr Command Options

Option

Qualifier

Description

/b

 

Matches the string pattern if it is at the beginning of a line.

/e

 

Matches the string pattern if it is at the end of a line.

/l

 

Interprets the strings literally. In other words, it does not use metacharacters.

/c

:"string"

Matches the entire text defined by string literally. Use this option to find a string consisting of two or more words separated by spaces.

/r

 

Interprets metacharacters in search strings as regular expressions. This option is the default unless the /l option is included.

/s

 

Searches the current directory and its subdirectories for matching files.

/i

 

Ignores uppercase and lowercase distinctions.

/x

 

Displays lines that match exactly.

/v

 

Displays only those lines that do not contain a match.

/n

 

Displays the line number before each line that matches.

/m

 

Displays only the file name of the file that contains a match.

/o

 

Displays the seek offset before each line that contains a match.

/g

:filename

filename is the name of the file that contains the search strings.

/f

:filename

filename is the name of the file that contains the file names to be searched.

The file(s) parameter contains the name of the file(s) that will be searched and can use wildcard characters such as * and ? to designate multiple files. For example, to search all files in a particular directory, enter *.*. If necessary, this parameter can include the drive designation and the path in addition to the file name.

By default findstr will display the lines of the file(s) that match the specified string. This display may vary based on the options selected, which are described in Table A.70.

FINDSTR COMMAND EXAMPLES

Suppose a user is looking for a particular email message with the subject "Mary's Retirement Party." To search all of her saved email messages, even those in the subdirectories, and display the file name that contains the message, she enters

findstr /s /i /m /c:"Mary's Retirement Party" *.*

Now suppose a user wants to display the numbers of the lines that contain the acronym "GOSIP" in the govt.txt file. He enters

findstr /n GOSIP govt.txt

  Previous section   Next section
Top