Use the FIND command to search for a specific string in a file or files and send the specified lines to your output device.
(you may prefer FINDSTR, a much more powerful version of FIND, which even supports regular expressions.)
Syntax: | ||
FIND [/V or /C][/I][/N] "string" [drive:][path]filename | ||
where: | ||
/V | Displays all lines not containing the string specified. | |
/C | Displays the count of lines containing the string. | |
/I | Ignores the case of characters when searching for the string. | |
/N | Displays the line numbers with the displayed lines. | |
/OFF[LINE] | Do not skip files with offline attribute set (only available in Windows XP and later versions). | |
"string" | Specifies the text string to find. | |
drive:\path | Specifies the location of the file or files to search. | |
filename | Specifies the name of the file to be searched. | |
If a path is not specified, FIND searches the text typed at the prompt or piped from another command. |
Note: | If the "string" contains any "special" characters (i.e. doublequote, ampersand, pipe, greater or less than, caret, percent sign) these characters need to be escaped: a literal percent sign must be replaced by a double percent sign, a literal doublequote by 2 doublequotes, the rest must be preceded by a caret.If the FIND command is placed within a code block (i.e. in parenthesis) it is safest to escape parenthesis inside the text string with carets too. |
So, with /C
, FIND
may be used for counting as well.
Use the FIND
command to check if your HTML files have a closing tag for each opening tag:
C:\>FIND /C /I "<TD" example.html
---------- example.html: 20 C:\>FIND /C /I "</TD" example.html
---------- example.html: 20 C:\>_
Combine it with FOR
to create this small batch file (for Windows NT or later, or OS/2), which should be called with an HTML file name as its only argument:
@ECHO OFF FOR %%A IN (A CODE FONT H1 H2 H3 P PRE TABLE TD TH TR) DO ( ECHO.%%A FIND /C /I "<%%A" %1 ECHO./%%A FIND /C /I "</%%A" %1 )
Notes: | 1: | In this example FIND /C will only display the number of lines it finds with the search string specified; it does not display the number of occurrences of the search string! |
2: | In "true" DOS batch files, no line should ever exceed 127 characters. |
FIND
returns an errorlevel 1 if the search string wasn't found (as of MS-DOS 6).
IsDev.bat is an example of a batch file depending on this feature.
Thanks to Robert Cruz, who provided me with details on escaping doublequotes in FIND
's search string.
He also provided this link to Microsoft's FIND command web page.
page last modified: 2016-09-19; loaded in 0.0017 seconds