How do I search files for a batch-file or command-line string?

A. The find command lets you search one file at a time for a string, but the findstr command is more versatile. This command has the following switches.

findstr \[/b\] \[/e\] \[/l\] \[/r\] \[/s\] \[/i\] \[/x\] \[/v\] \[/n\] \[/m\] \[/o\] \[/f:file\] \[/c:string\] \[/g:file\] \[strings\] \[\[drive:\]\[path\]filename\[ ...\]\]

The following table explains each parameter.

Parameters Meaning
/b Matches pattern if at the start of a line
/e Matches pattern if at the end of a line
/l Searches literally
/r Uses text as a regular expression (default)
/s Searches current directory and all subdirectories
/i Ignores case
/x Selects lines that are an exact match
/v Selects lines that don’t match
/n Displays the line number before the matched line
/m Displays only the matching filenames
/o Displays the offset of the match before the matched line
/g:<file> Gets the search string from the specified file (e.g., /g:argument.txt)
/c:"<string>" Uses text as a literal (e.g., /c:"string")
/f:<file> Gets the file list from the specified file (e.g., /f:filelist.txt)
strings Denotes the search string (in double quotes if multiple words)
files Shows the files to search

Use spaces to separate multiple search strings unless you use /c.

The command

findstr "Windows NT FAQ" ntfaq.html

would search for Windows, NT, or FAQ in ntfaq.html. The command

findstr /c:"Windows NT FAQ" ntfaq.html

would search for Windows NT FAQ in ntfaq.htm.

Discuss this Article 2

gggggggggggggggggggggggggggggggggggggggggggggggggggguiiiiiii (not verified)
on Oct 28, 2003
Your Comments (required):

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• The Microsoft
Technology Roadmap
• Office 365 Implementation
• Hyper-V Optimizing
• Windows 8 Deployment
and much more!

Come See Paul Thurrott & Rod Trent in Person!

Early Registration Now Open

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.