Home > Commands A-M > Commands F

FIND


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Searches for a specific string of text in a file or files.

After searching the specified files, FIND displays any lines of text that contain the specified string.


Syntax

FIND /?

FIND [[/C] | [/N]] [/I] [/OFF[LINE]] [/V] "string" [[drive:][path]filename [...]]


Parameters
drive: ( path filename [...] v2.0 Win95 NT3.5)
Specifies the location and name of the file (or files) in which to search for the specified string.
"string" (v2.0 Win95 NT3.5)
Specifies the group of characters you want to search for. You must enclose the text for string in quotation marks.

Switches
/? (NTXP)
Display help.
/C (v2.0 Win95 NT3.5)
Displays only a count of the lines that contain the specified string.
/I (v5.0 Win95 NT3.5)
Specifies that the search is not to be case-sensitive.
/N (v2.0 Win95 NT3.5)
Precedes each line with the file's line number.
/OFF[LINE] (NTXP ONLY)
Do not skip files with offline attribute set.
/V (v2.0 Win95 NT3.5)
Displays all lines not containing the specified string.

Related

FC - Compare files.
FINDSTR - Search for strings in files.
MUNGE - Find and Replace text within file(s).
Equivalent Linux BASH commands:
grep - Search file(s) for lines that match a given pattern.
gawk - Find and Replace text within file(s).
tr - Translate, squeeze, and/or delete characters.


Notes
Specifying a string

Unless you specify the /I switch, FIND searches for exactly what you specify for string. For example, to the FIND command the characters "a" and "A" are different. If you were to use the /I switch, however, FIND would ignore case and search for "a" and "A" as if they were the same character.

If the string you want to search for contains quotation marks, you must use two quotation marks for each quotation mark contained within the string.


Using FIND as a filter

If you omit a filename, FIND acts as a filter, taking input from the operating system standard source (usually the keyboard, a pipe, or a redirected file) and displaying any lines that contain the string.


Using wildcards with FIND

You cannot use wildcards (* and ?) in filenames or extensions that you specify with the FIND command. To search for a string in a set of files you specify with wildcards, you can use the FIND command in a FOR command.


Using the /V or /N switch with the /C switch

If you specify the /C and /V switches in the same command, FIND displays a count of the lines that do not contain the specified string. If you specify the /C and /N switches in the same command, FIND ignores the /N switch.


Using FIND in files with carriage returns

The FIND command does not recognize carriage returns. When you use FIND to search for text in a file that includes carriage returns, you must limit the search string to text that can be found between carriage returns -- that is, a string that is not likely to be interrupted by a carriage return. For example, FIND does not report a match for the string "tax file" wherever a carriage return occurs between the word "tax" and the word "file".


Examples

To display all lines from the file PENCIL.AD that contain the string "Pencil Sharpener", type:

    FIND "Pencil Sharpener" PENCIL.AD

To find a string that contains text within quotation marks, you must enclose the entire string in quotation marks and, in addition, use two quotation marks for each quotation mark contained within the string, as shown in the example:

    FIND "The scientists labeled their paper ""for discussion only."" It is not a final report."" REPORT.TXT

If you want to search for a set of files, you can use the FIND command with the FOR command. Using this method to search the current directory for files that have the extension .BAT; in each file found, the command searches for the string "PROMPT":

    for %f in (*.bat) do FIND "PROMPT" %f

Suppose you want FIND to search your hard disk to find and display the filenames on drive C that contain the string "CPU". To do this, you can use the pipe (|) to direct the results of a DIR command to FIND, as shown in:

    DIR C:\ /S /B | FIND CPU"

Before using a pipe for redirection, you should set the TEMP environment variable in your AUTOEXEC.BAT file.

Since FIND searches are case-sensitive and since DIR produces uppercase output, you must either type the string "CPU" in uppercase letters or use the /I switch with FIND.

Excluding the FIND syntax

    FIND " FIND don't found ""FIND""" < FIND.fnd | FIND /V ""Find""

The above DOS syntax would search any file named "find.fnd" and the first FIND command would search this file for:

    FIND don't found "FIND"

This is commonly used, nothing special and it's descriped above. But it's sometimes possible (with use of two percent marks (%)), to get rid of the pipe ((|) along with the second FIND command).

    :: Find any Space character located before an asterisk, --- *--
    @FIND " % %*" < %0
    :: Find any Space character located after as asterisk,  --* ---
    @FIND " *% % " < %0

The FIND syntax itself, would be excluded from the output of %0.

You can use ERRORLEVEL parameter of If command-line in a batch program to process exit codes returned by FIND:

    IF (%1)==() @ECHO Syntax: %0 filename
    IF EXIST %1 TYPE %1 | FIND /V "http://" > Nul
    IF EXIST %1
    IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 EDIT %1

Errorlevels
Availability
External
DOS
v2.0 v2.01 v2.05 v2.10 v2.11 v2.11R v2.12 v2.2 v2.25 v3.0 v3.20 v3.05 v3.1 v3.21 v3.25 v3.30 v3.3A v3.3R v3.3T v3.31 v3.40 v4.0 v4.01 v4.01A v5.0 v5.0A v5.00.02 v5.001A v5.01 v5.02 v6.0 v6.10 v6.2 v6.21 v6.22 v6.23 v7.00 v7.0R1 v7.10 v8.00
Windows
Win95 Win98 WinME
Windows NT
NT3.5 NT4 NT2000 NTXP NT2003