1

How to Search the Contents of Multiple Files at Once

raccoon-mascot

Once in a while the need arises to find a needle in a haystack. Today I had to find a string of characters embedded somewhere in a stack of 38 folders and 400 files. To accomplish this task manually is not realistic.

Thankfully, there are two simple ways to do this in Windows.

findstr

Windows contains a command line tool called findstr. It searches for patterns of text in files. It can be used to search folders and sub-folders recursively, meaning that it will search the contents of all files within a folder and also all sub-folders it contains.

It is quite simple to use findstr. Go to Start > All Programs > Accessories and open a Command Prompt.

Within Command Prompt, type and enter the following command:

The above command will look for the string "window_placement" in the C:\Users folder and all its sub-folders. The meaning of this specific string can be further researched in a previous post of mine.

The /s parameter specifies that findstr search for matches in all sub-folders as well as the explicitly specified folder.

The /m parameter specifies that findstr only outputs the file name if a file contains a match.

The \*.* expression after the C:\Users folder means that findstr will look into all available files to match the string.

You can also copy-paste commands to the Command Prompt’s window. However, when pasting them, you have to right-click with your mouse within the Command Prompt window and select Paste from the drop-down menu.

Findstr will display its findings in the Command Prompt window.

To print the output into a file, use the following variation of the previous command:

I am deliberately using the D: drive for creating a text file because the C: drive’s root folder is usually inaccessible for such uses.

grep

There is a similar command line tool originally developed for the Unix operating system, that is much faster. It is called grep (global regular expression print) but, unfortunately, it is not part of Windows out-of-the-box.

You can download and install grep from the great GnuWin32 Packages.

To use grep, again go to Start > All Programs > Accessories and open a Command Prompt.

Within Command Prompt, type and enter the following command:

The -I command line option specifies that grep skip all binary files.

The -r command line option specifies that grep search all sub-folders in C:\Users recursively (the same as /s in findstr but without the superfluous \*.* expression).

The -s command line option specifies that grep suppress all error messages, i.e. it doesn’t bother the user with an error every time a file is inaccessible (unlike findstr).

To print the output into a file, use the following variation of the previous command:

Comparison

Grep is much faster than findstr.

It took grep to find all instances of the string I searched for only 24 seconds. The same task took findstr 4 minutes and 52 seconds. So grep is actually more than 12 times faster than findstr.

The reason for the massive difference in task completion time is that grep can be configured not to search for strings in binary files. So grep simply skips over every binary file after analyzing the first 32 kilobytes of its content — it is clear after that much data that a file is binary and not readable text.

To find out more about the many command line options of both command line tools, click here for findstr and click here for grep.

Liked this post?

Subscribe to our newsletter to receive early notification of new posts and deals:

Next Post »« Previous Post

Comments 1

  1. Peter Post
    Author

    Someone suggested that I also try AstroGrep, an open source freeware utility that doesn’t use the Command Prompt because it has its own window.

    I tried this utility and here are the results.

    It took AstroGrep an obscene 14 minutes and 54 seconds to find all occurrences of the search keyword from the above post.

    At one point AstroGrep needed more than 300MB of memory, although most of the time it needed “only” 123MB. In comparison findstr needed up to 1.5MB and grep up to 3.5MB — consistently.

    If you are really afraid of using Command Prompt and can’t look up a few command line options and you have a lot of time and computer memory to spare, you may as well use this utility.

    Otherwise, stick to the grep command line tool.

Leave a Reply

Your email address will not be published. Required fields are marked *