findstr - Advanced Text Search
The find command can only do simple string searches, but the findstr command supports advanced pattern searches using regular expressions.
This article explains findstr from the basics to regular expressions, multiple conditions, and pipes, with sample code.
Basic syntax of findstr
FINDSTR [options] "search_string" [file_name]
Basic example
Search a file for lines containing a specific string.
findstr "error" log.txt
Main options
| Option | Description |
|---|---|
/I | Case-insensitive search |
/S | Search subfolders recursively |
/R | Treat search strings as regular expressions |
/L | Treat search strings literally |
/C:"string" | Search for the string as-is, including spaces (literal by default) |
/V | Show lines that do not match (exclusion) |
/N | Show line numbers of matching lines |
/M | Show only the names of files with a match |
/B | Match the pattern at the beginning of a line |
/E | Match the pattern at the end of a line |
/X | Show only lines that match exactly as a whole |
/G:file | Read search strings from a file |
/D:dirs | Search a semicolon-separated list of directories |
A search string given without /C: is interpreted as a regular expression when it contains special characters such as . or *. To search for . or $ literally, specify /L or escape them with a backslash, as in \..
Case-insensitive search (/I)
findstr /I "warning" log.txt
Warning, WARNING and warning all match.
Searching for strings with spaces (/C:)
Use the /C: option to search for a string that contains spaces.
findstr /C:"disk full" log.txt
If you write findstr "disk full" log.txt without /C:, lines containing “disk” or “full” are matched (an OR search). Use /C: to search for the exact string including the space.
Showing non-matching lines (/V)
The /V option shows lines that do not match the search string.
findstr /V "^rem " script.cmd
Showing line numbers (/N)
findstr /N "TODO" main.cmd

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Searching with regular expressions
FINDSTR supports basic regular expressions.
Supported regular expressions
| Symbol | Meaning | Example |
|---|---|---|
. | Any single character | h.t → hot, hat |
* | Zero or more of the preceding character | ab*c → ac, abc, abbc |
^ | Beginning of a line | ^echo |
$ | End of a line | error$ |
[abc] | Any one character in the brackets | [aeiou] → vowels |
[a-z] | Any one character in the range | [0-9] → digits |
[^abc] | Any one character not in the brackets | [^0-9] → non-digits |
\< | Beginning of a word | \<the |
\> | End of a word | end\> |
Regular expression examples
findstr /R "^echo" script.cmd
findstr /R "[0-9]" data.txt
findstr /R "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" log.txt
FINDSTR’s regular expressions are very basic. Extended features such as + (one or more), ? (zero or one), | (OR) and () (grouping) are not supported.
Searching with multiple conditions
OR search (lines containing any of the strings)
Separate the search strings with spaces to show lines that match any of them.
findstr /I "error warning" log.txt
AND search (lines containing all of the strings)
FINDSTR has no AND option, so chain commands with a pipe to narrow the results.
findstr /I "error" log.txt | findstr /I "database"
Checking the result
FINDSTR returns exit code (error level) 0 when a matching line is found and 1 when none is found. You can use this to check existence in a batch file.
@echo off
findstr /I /C:"error" log.txt >nul
IF %ERRORLEVEL% EQU 0 (
echo Errors are recorded.
) ELSE (
echo No errors are recorded.
)
Searching multiple files
Using wildcards
findstr "error" *.txt
Including subfolders (/S)
findstr /S "TODO" *.cmd
Showing only names of matching files (/M)
findstr /M /S "password" *.txt
Only the names of files containing the search string are shown, not their contents.
Combining with pipes
Combine findstr with the pipe | to filter the output of other commands.
tasklist | findstr /I "chrome"
netstat -an | findstr "443"
findstr vs. find
| Feature | find | findstr |
|---|---|---|
| Regular expressions | × | ○ |
| Recursive search | × | ○ (/S) |
| Multiple conditions | × | ○ (space-separated, /G) |
| Line numbers | ○ (/N) | ○ (/N) |
| Count matching lines | ○ (/C) | × |
| Case-insensitive | ○ (/I) | ○ (/I) |
| Exclusion | ○ (/V) | ○ (/V) |
FINDSTR may not handle UTF-8 or UTF-16 files correctly. If results are not as expected for files containing Japanese or other non-ASCII text, check the file’s character encoding. See also the article on encoding.
Use find for simple fixed-string searches, and findstr when you need regular expressions or subfolder searches.
Summary
This article explained how to use the findstr command.
FINDSTR is a powerful command for advanced text searches that find cannot handle, such as regular expressions, recursive search of subfolders, and pipe integration.