findstr - Advanced Text Search

Maintained on
Table of Contents

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
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>findstr "error" log.txt
2026-03-10 10:15:32 error: File not found
2026-03-10 10:20:45 error: Connection timed out
C:\users\user>

Main options

OptionDescription
/ICase-insensitive search
/SSearch subfolders recursively
/RTreat search strings as regular expressions
/LTreat search strings literally
/C:"string"Search for the string as-is, including spaces (literal by default)
/VShow lines that do not match (exclusion)
/NShow line numbers of matching lines
/MShow only the names of files with a match
/BMatch the pattern at the beginning of a line
/EMatch the pattern at the end of a line
/XShow only lines that match exactly as a whole
/G:fileRead search strings from a file
/D:dirsSearch a semicolon-separated list of directories
Regular expression or literal?

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
Without /C:

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.

Hide
findstr /V /B /I /C:"rem " script.cmd

Showing line numbers (/N)

findstr /N "TODO" main.cmd
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>findstr /N "TODO" main.cmd
15:rem TODO: add error handling
42:rem TODO: implement logging
C:\users\user>

Searching with regular expressions

FINDSTR supports basic regular expressions.

Supported regular expressions

SymbolMeaningExample
.Any single characterh.t → hot, hat
*Zero or more of the preceding characterab*c → ac, abc, abbc
^Beginning of a line^echo
$End of a lineerror$
[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 wordend\>

Regular expression examples

Lines
findstr /R "^echo" script.cmd
Lines
findstr /R "[0-9]" data.txt
Pattern
findstr /R "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" log.txt
Limits of the regular expressions

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"

If you only need lines where “database” comes after “error”, you can search once by joining them with the regular expression .* (the order is fixed).

findstr /I /R "error.*database" log.txt

Checking the result

FINDSTR returns exit code (error level) 0 when a matching line is found and 1 when none is found (it may return 2 or higher on errors such as a file that cannot be opened). You can use this to check existence in a batch file.

check_log.cmd
@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

Search
findstr "error" *.txt

Including subfolders (/S)

Search
findstr /S "TODO" *.cmd
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>findstr /S "TODO" *.cmd
scripts\backup.cmd:rem TODO: add logging
scripts\deploy.cmd:rem TODO: check the test environment
tools\cleanup.cmd:rem TODO: update the exclusion list
C:\users\user>

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.

Find
tasklist | findstr /I "chrome"
Find
netstat -an | findstr "443"

findstr vs. find

Featurefindfindstr
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)
Character encoding limits

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.

Tip

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.

Practice question

Practice Problem

What happens when you run findstr "disk full" log.txt without the /C: option?

Choose one answer.

  1. Lines containing "disk" and lines containing "full" are both shown (OR search)
  2. Only lines containing the string "disk full" are shown
  3. An error occurs and the search fails
#Command Prompt #Batch Files #FINDSTR #Regular Expressions #Text Search