Programming Field

Find - DOS/Command Prompt Reference

Searches for the specified string line by line from the file content or standard input, and displays the search result.

[Windows 2000 or later] By using ‘Findstr’ you can search for a text from files recursively or search with simple regular expressions.

* In Cygwin environment, or an environment where the directory containing Git for Windows or etc. executables is set to PATH, ‘find(.exe)’ may refer to these Linux programs. To check whether Windows command Find.exe runs, execute ‘find /?’ and see whether the command help is displayed.

Syntax

find[.exe] [/V] [/C] [/N] [/I] [/OFF[LINE]]
    "<text>" [[<drive>:][<path>]<file-name> ...]

Options

/V Searches for the line that does NOT include the specified string.
/C Prints the matched line count when finish searching. Number of lines including string when /V is not specified, or number of lines not including string when /V is specified, is displayed.
/N Displays the line number when showing matched lines. This option has no effect when /C is specified.
/I Ignores case when searching for strings.
/OFF[LINE] [Windows NT series] Searches (does not skip) files with ‘offline attribute’ set.
"<text>" Specifies the string to find. The string must be enclosed in ‘"’ like ‘"apple"’.
<drive>:, <path>, <file-name>

Specifies the file name to search. When the drive and the path is omitted, Find searches for the file in the current directory. You can specify multiple files.

[Windows NT series] Wildcards are allowed as a file name.

[MS-DOS, Windows 95/98/Me] You cannot use wildcards.

Details

This command searches for the string from the file. This does not search for files (by specifying the name). To search for files, use Dir command or For command.

Find returns following exit codes. You can use If and Errorlevel to do conditional processings in batch files.

CodeDetails
0Searching finished successfully, and one or more strings were found in the file. When multiple files are specified, this indicates the strings were found in one or more files.
1Searching finished successfully, but strings were not found in the file. When multiple files are specified, this indicates the strings were not found in all files.
2An error occurred while searching. (The search result is unknown.)

* When ‘/V’ is specified, the meaning of ‘found’ is reversed; ‘0’ is returned if at least one of the target files has at least one line which does not include the string, or ‘1’ is returned if all target files are filled with the lines which include the string.

Samples

Sample

find /n "the" document.txt

Searches for ‘the’ string from document.txt. The search result will be as follows:

---------- document.txt
[5]and he got the best prize.

See also