Using the Findstr Command

by Barry Dysert
(last updated December 19, 2016)

The "Find" command is usually adequate for most of your needs (see the tip entitled Using the Find Command), but there is also a "Findstr" command that is more powerful for those times when you need to find strings under certain circumstances. In its simplest form, Findstr looks like Find:

C:\> FINDSTR "document" Barry?.tmp

Findstr accepts several more switches than Find, and you can see a complete list by typing the following:

C:\> FINDSTR /?

The first difference you'll notice between the output of Find vs. Findstr is that the latter prefixes each found line with the name of the file in which it is found: (See Figure 1.)

Figure 1. Findstr displays the file names in-line with the found string.

There are over a dozen switches available with Findstr, but instead of going through them all I'll just tell you some of the more important (imo) functions available over what the Find command has. The first thing I'll mention is that you can search for multiple strings at the same time. If the search string you're looking for contains spaces, Findstr will assume you want to find string1 or string2 or string3, etc. For example, if you want to find all lines that contain either "document" or "text" you just separate the words with a space like so: (See Figure 2.)

Figure 2. Searching for two strings at once.

Another nice feature is that Findstr can search files in subdirectories. Let's say that I have Barry1.tmp in my \Temp directory and Barry2.tmp in my \Temp\Temp2 directory. From the \Temp directory I can use the "/S" switch to tell Findstr to search subdirectories. It looks like this: (See Figure 3.)

Figure 3. Using /S to search subdirectories.

There is a good deal more functionality that I could show you, but a final one really bears special notice. That is the concept of searching using Regular Expressions. "Introduction to Regular Expressions" is covered under another Tip, but I want to show you how a regular expression can be used in Findstr.

Let's say you want to search for the word "document", but only those occurrences that are followed by a punctuation mark like a comma or a period. The regular expression for this type of search is "document[,.]". You indicate to Findstr that your search string is using a regular expression by specifying the "/R" switch, and let's add the "/N" switch so we see line numbers. What you end up with is this: (See Figure 4.)

Figure 4. Search using a Regular Expression.

There is still more to Findstr, but I hope I've whetted your appetite enough to where you'll start using it when you need to do some fancy searching for strings in files.

 This tip (13505) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Using Batch Files, Part 1

This tip is part of a series that shows you how to create and use Windows batch files. It introduces a few commands and the ...

Discover More

Adding Locations to the Search Index

You can fine-tune Indexed Searches by adding locations to the search index. This tip tells you how.

Discover More

Improve Performance by Turning Off 3D Effects

Windows does a lot of nuancing to provide the user with a pleasing visual experience. All this attention to detail, though, ...

Discover More
More WindowsTips

Moving Files Using the Command Line

The MOVE command can be a timesaver over trying to do the similar sort of thing with Windows Explorer. You can move hundreds ...

Discover More

Displaying the Command Prompt Window

You may have the need to perform repetitive operations or group several commands together to run as a batch. In such cases, ...

Discover More

Using the Sort Command

Sorting data is a common task even of end users. Fortunately, The Windows command line provides us with a Sort utility so we ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.