FIND

Search for a text string in a file & display all the lines where it is found.

Syntax
      FIND [/V] [/C] [/N] [/I] "string" [pathname(s)]
Key
"string" The text string to find (must be in quotes).
[pathname] A drive/file(s) to search (wildcards accepted). /V Display all lines NOT containing the specified string.

/C Count the number of lines containing the string.
/N Display Line numbers.
/I Ignore the case of characters when searching for the string. [/off[line]] Do not skip files that have the offline attribute set.

If a [pathname] is not specified, FIND will prompt for text input or will accept text piped from another command.
(use CTRL-Z to end manual text input)

If searching for text that contains double quote characters " , they must be escaped by doubling to ""
This is in addition to enclosing the entire string in quotation marks: "The ""main"" event"

The FIND command will output a string of 10 dashes ---------- followed by the filename being searched, followed by any matching lines of text in the file.

Limitations

Find does not support wildcards, use FINDSTR instead.

Although FIND can be used to scan large files, it will not detect any string that is positioned more than 1070 characters along a single line (with no carriage return) This makes it of limited use in searching binary or XML file types.

An empty string "" will normally match nothing, with the/v flag reversing the test, to match everything, but this will fail for any lines longer than 4091 characters long.

For lines longer than 4091 characters use FINDSTR which will cope with up to 8191 characters.

Errorlevel

FIND will return an ErrorLevel as follows:

0 String found in at least one of the files.
1 String not found
2 If any files in the list do not exist or if no files match a wildcard mask. An invalid switch is given.

Examples

If names.txt contains the following:

  Joe Bloggs, 48 Main St, Dunoon 
  Shameika James, 127 Scotland Street, Edinburgh

To search for "Shameika" in names.txt

C:\> FIND "Shameika" names.txt

---------- NAMES.TXT Shameika James, 127 Scotland Street, Edinburgh

If you want to pipe the output from a command into FIND use this syntax:

C:\> TYPE names.txt | FIND "Shameika"

You can also redirect like this:

C:\> FIND /i "Shameika" < names.txt >logfile.txt

To search a folder for files that contain a given search string:

C:\> FOR %G IN (*.txt) do (find /n /i "SearchWord" "%G")

Count the number of lines in a file (like wc -l on unix).
The empty string "" is treated as never matching. The /v flag reverses the test, so now it matches everything and then /c returns the count:

C:\> TYPE myfile.txt | FIND "" /v /c

“For my art, there is a common theme most of the time: it is using the things we can see to search for the world we cannot see” ~ Cai Guo-Qiang

Related commands

FINDSTR - Search for strings in files, supports regular expressions.
ATTRIB - Find filename, rather than searching the file contents.
WHERE - Locate and display files in a directory tree.
Escape chars, delimiters and quotes
Redirection - Spooling output to a file, piping input.
Advanced Query Syntax (AQS) in Windows File Explorer.
Agent Ransack - Free File Searching Utility.
Equivalent PowerShell: Where-Object - Filter objects passed along the pipeline.
Equivalent bash command (Linux): awk/gawk - Find and Replace text within file(s).


 
Copyright © 1999-2024 SS64.com
Some rights reserved