previous | start | next


FINDSTR

Searches for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/F:file]
        [/C:string] [/G:file] [strings] [[drive:][path]filename[ ...]]

  /B        Matches pattern if at the beginning of a line.
  /E        Matches pattern if at the end of a line. Watch out for spaces 
		echo ab | findstr /E "b" fails. echo ab| findstr /E "b" works
  /L        Uses search strings literally.
  /R        Uses search strings as regular expressions. This switch is not
		required; findstr will interpret 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 filename if a file contains a match.
  /O        Prints character offset before each matching line.
  /P        Skip files with non-printable characters
  /F:file   Reads file list from the specified file(/ stands for console).
  /C:string Uses specified string as a literal search string. Still a regexp, 
		but now can include spaces.
  /G:file   Gets search strings from the specified file(/ stands for console).
  strings   Text to be searched for.
  [drive:][path]filename
            Specifies a file or files to search.

Use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y.

Findstr is capable of finding the exact text you are looking for in any ASCII file or files. Sometimes, however, 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 occurance 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 occurances 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 (*) characters .* 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 occurances 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 occurances of lines that contain the word FOR, preceeded by any number of spaces, (as in a computer program loop) and to include the line number where each occurance 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 criteria 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 and search the files listed in FILELIST.TXT 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.*" *.*

Piping from echo and using the && or || operators is very useful in batch files.

echo %file%|findstr /E/I "bat com exe" && echo %file% is a program

Back to the top of this page

 



previous | start | next