FINDSTR

Search for strings in files.

Syntax
      FINDSTR [options] [/F:file] [/C:string] [/G:file]
        [/D:DirList] [/A:color] [/OFF[LINE]] [string(s)] [pathname(s)]

      FINDSTR [options] [/F:file] [/R] [/G:file]
        [/D:DirList] [/A:color] [/OFF[LINE]] [string(s)] [pathname(s)]
Key
   string      Text to search for.
   pathname(s) The file(s) to search. 
   /C:string   Use string as a literal search string.
   /R          Use string as a regular expression.
   /G:file     Get search string from a file (/ stands for console).
   /F:file     Get a list of pathname(s) from a file (/ stands for console).
   /A:color    Display filenames in colour (2 hex digits)
   /d:dirlist  Search a comma-delimited list of directories.

options may be any combination of the following switches:
/I Case-insensitive search. /S Search subfolders. /P Skip any file that contains non-printable characters /OFF[LINE] Do not skip files with the OffLine attribute set. /L Use search string(s) literally. /B Match pattern if at the Beginning of a line. /E Match pattern if at the END of a line. /X Print lines that match exactly. /V Print only lines that do NOT contain a match.
/N Print the line number before each line that matches. /M Print only the filename if a file contains a match. /O Print character offset before each matching line.

If more than one file is searched, the results will be prefixed with the filename where the text was found.

Searching for spaces: when the search string contains multiple words, separated with spaces, then FINDSTR will return lines that contain either word (OR).
A literal search (/C) will reverse this behaviour and allow searching for a phrase or sentence.

Regular Expressions
(Searching for patterns of text)

The FINDSTR syntax notation can use the following metacharacters which have special meaning either as an operator or delimiter.

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

Metacharacters are most powerful when they are used together. For example, the combination of the wildcard character (.) and repeat (*) character is similar in effect to the filename wildcard (*.*)

.*         Match any string of characters

The .* expression may be useful within a larger expression, for example a.*b will match any string beginning with A and ending with B.

FINDSTR does not support alternation with the pipe character (|) multiple Regular Expressions can be separated with spaces, just the same as separating multiple words (assuming you have not specified a literal search with /C) but this may not be useful if the regex itself contains spaces.

Literal search

Search a text file Demo.txt that contains the following

The quick brown fox
The really ^brown^ fox

A literal search will ignore any special meaning for the search characters:

FINDSTR /C:"^brown" Demo.txt

Using a script file

Multiple search criteria can be specified with a script file /G.
Multiple FileNames to search can be specified with /F.

When preparing a source or script file, place each filename / search criteria on a new line.
If several filenames are to be searched they must all exist or FINDSTR will fail with an error.

For example: to use the search criteria in Criteria.txt to search the files listed in Files.txt:

FINDSTR /g:Criteria.txt /f:Files.txt

Examples:

Search for "granny" OR "Smith" in the files Apples.txt or Pears.txt

FINDSTR "granny Smith" Apples.txt Pears.txt

Search for "granny Smith" in Contacts.txt (effectively the same as the FIND command)

FINDSTR /C:"granny Smith" Contacts.txt

Search every file in the current folder and all subfolders for the word "Smith", regardless of upper/lower case, note that /S will only search below the current directory:

FINDSTR /s /i smith *.*


Search all the text files in the current folder for the string "fiona", display the filenames in White on Green.

FINDSTR /A:2F /C:fiona *.txt

To find every line in novel.txt containing the word SMITH, preceeded by any number of spaces, and to prefix each line found with a consecutive number:

FINDSTR /b /n /c:" *smith" novel.txt


Finding a string only if surrounded by the standard delimiters
Find the word "computer", but not the words "supercomputer" or "computerise":

FINDSTR "\<computer\>" C:\work\inventory.txt


Find any words that begin with the letters 'comp', such as 'computerise' or 'compete'

FINDSTR "\<comp.*" C:\work\inventory.txt

Find any positive integers in the file sales.txt and include any lines that are a zero (0):

FINDSTR /r "^[1-9][0-9]*$ ^0$" Sales.txt

Errorlevel

When an item is not found FINDSTR will return an %ERRORLEVEL% > 0

Echo 12G6 |FindStr /R "[0-9]"
If %ERRORLEVEL% EQU 0 echo The string contains one or more numeric characters

Echo 12G6 |FindStr /R "[^0-9]"
If %ERRORLEVEL% EQU 0 echo The string contains one or more non numeric characters

Bugs
In early versions of FindStr /F:file a path length of more than 80 chars will be truncated.
Regular Expression support is limited.

“Twenty years from now, you will be more disappointed by the things you didn't do than by the ones you did do. So throw off the bowlines, sail away from the safe harbour. Catch the trade winds in your sails. Explore. Dream. Discover” ~ Mark Twain


Related:

FIND - Search for a text string in a file.
VBScript: Find and Replace
Powershell: Regular Expressions
Powershell: Where-Object - Filter objects passed along the pipeline.
Equivalent bash command (Linux): grep - Search file(s) for lines that match a given pattern



Back to the Top

© Copyright SS64.com 1999-2012
Some rights reserved