Search from the Windows command prompt

21 May 2023

When you need to search within text files from Windows batch files, you can use either the find or findstr command. Findstr supports a limited version of regular expressions. You can also automate certain tasks based on the search results.

The 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 FIND command in Windows 11

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:

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:

The FINDSTR command in Windows 11

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. Shameless plug: Aba Search and Replace supports command-line options as well, allowing you to search from the command prompt and replace text from Windows batch files.

Aba Search and Replace screenshot

Replacing text in several files used to be a tedious and error-prone task. Aba Search and Replace solves the problem, allowing you to correct errors on your web pages, replace banners and copyright notices, change method names, and perform other text-processing tasks.

This is a blog about Aba Search and Replace, a tool for replacing text in multiple files.