Skip navigation

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.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish