Search from the Windows Command Prompt
When you need to search in text files from batch files, you can use either the FIND or FINDSTR commands. You can also automate certain tasks based on the search results.
Join the DZone community and get the full member experience.
Join For FreeThe FIND Command
To search for text in multiple files from the Windows command prompt or batch files, you can use the FIND command, which has been present since the days of MS DOS and is still available in Windows 11. It's similar to the Unix grep
command, but does not support regular expressions. If you want to search for the word borogoves
in the current directory, please follow this syntax:
find "borogoves" *
Note that the double quotes around the pattern are mandatory. If you are using PowerShell, you will need to include single quotes as well:
find '"borogoves"' *
Instead of the asterisk (*
), you can specify a file mask such as *.htm?
. The find
command displays the names of the files it scans, even if it doesn't find any matches within these files:
The search is case-sensitive by default, so you typically need to add the /I
switch to treat uppercase and lowercase letters as equivalent:
find /I "<a href=" *.htm
If you don't specify the file to search in, find
will wait for the text input from stdin, so that you can pipe output from another command. For example, you can list all copy commands supported in Windows:
help | find /i "copy"
Another switch, /V
, allows you to find all lines not containing the pattern, similar to the grep -v
command.
In batch files, you can use the fact that the find
command sets the exit code (errorlevel) to 1 if the pattern is not found. For instance, you can check if the machine is running a 64-bit or 32-bit version of Windows:
@echo off rem Based on KB556009 with some corrections reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" /v "Identifier" | find /i "x86 Family" > nul if errorlevel 1 goto win64 echo 32-bit Windows goto :eof :win64 rem Could be AMD64 or ARM64 echo 64-bit Windows
The FINDSTR Command: Regular Expression Search
If you need to find a regular expression, try the FINDSTR
command, which was introduced in Windows XP. For historical reasons, findstr
supports a limited subset of regular expressions, so you can only use these regex features:
- The dot
.
matches any character except for newline and extended ASCII characters. - Character lists
[abc]
match any of the specified characters (a
,b
, orc
). - Character list ranges
[a-z]
match any letter froma
toz
. - The asterisk (
*
) indicates that the previous character cane be repeated zero or more times. - The
\<
and\>
symbols mark the beginning and the end of a word. - The caret (
^
) and the dollar sign ($
) denote the beginning of and the end of a line. - The backslash (
\
) escapes any metacharacter, allowing you to find literal characters. For example,\$
finds the dollar sign itself.
Findstr does not support character classes (\d
), alternation (|
), or other repetitions (+
or {5}
).
The basic syntax is the same as for the FIND
command:
findstr "\<20[0-9][0-9]\>" *.htm
This command finds all years starting with 2000 in the .htm
files of the current directory. Just like with find
, use the /I
switch for a case-insensitive search:
FINDSTR Limitations and Quirks
Character lists [a-z]
are always case-insensitive, so echo ABC | findstr "[a-z]"
matches.
The space character works as the alternation metacharacter in findstr
, so a search query like findstr "new shoes" *
will find all lines containing either new
or shoes
. Unfortunately, there is no way to escape the space and use it as a literal character in a regular expression. For example, you cannot find lines starting with a space.
Syntax errors in regular expression are ignored. For instance, findstr "[" *
will match all lines that contain the [
character.
If the file contains Unix line breaks (LF), the $
metacharacter does not work correctly. If the last line of a file lacks a line terminator, findstr
will be unable to find it. For example, findstr "</html>$" *
won't work if there is no CR+LF after </html>.
Early Windows versions had limitations on line length for find
and findstr
, as well as other commands. The recent versions lifted these limits, so you don't have to worry about them anymore. See this StackOverflow question for findstr
limitations and bugs, especially in early Windows versions.
The findstr command operates in the OEM (MS DOS) code page; the dot metacharacter does not match any of the extended ASCII characters. As the result, the command is not very useful for non-English text. Besides that, you cannot search for Unicode characters (UTF-8 or UTF-16).
Conclusion
You can learn about other switches by typing findstr /?
or find /?
. For example, the additional switches allow you to search in subdirectories or print line numbers. You can also refer to the official documentation.
In general, the find
and findstr
commands are outdated and come with various quirks and limitations.
Published at DZone with permission of Peter Kankowski. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments