Monday, March 08, 2010

Using the Windows Findstr Command to Find Text, Files and Folders

Currently, I like the findstr command better than other ways to search files in Windows.

Prerequisite: Open Command Window Here
For XP, it's part of Microsoft PowerToys for Windows XP.

For Vista and Windows 7, apparently you hold down the hold down the shift key and right click.

Finding File Contents
Once you have a command window open at the directory you want to search within, you  can do something like this, which will return all lines containing "MyText" in all subdirectories of the current one, case-insensitive, and only searching files that contain printable characters:

findstr /s /i /p "MyText" *.*

It turns out that "MyText" is treated as a regular expression, which is great. But if you don't want it treated as a regular expression, you can use the /l or /c: options.

Finding File & Folder Names
Instead of giving findstr files to search, you can pipe text to it from another command, so to search for file or folder names, you can give it the results of the dir command to search:

dir /s /b | findstr /i "MyFileName.txt"

Combining the Two
Say you want to find all the lines containing "MyText", except you don't want those that are part of a subversion directory. You can do this, where the /v option means only return lines that don't match:

findstr /s /i /p "MyText" *.* | findstr /v /l ".svn"