Wednesday, June 15, 2011

Using Batch File or Command Line to recursively find all files that have a filename that contains a particular string

It sounds like a tall order at first, but then it also sounds like something that should be built into Windows. In particular, I am using Windows 7, but I assume it works on XP as well.

The solution is actually quite simple and is indeed built into Windows 7.

dir /s /b *my string*

This will give you something similar to:

c:\temp\This is a test of my string.txt

c:\mydocs\My string could be found here.txt

As shown above it will search the current directory and all directories below the current one for files that contain ‘my string’. You can also specify something like this to specify the directory you want to start searching in regardless of your current directory.

dir /s /b “c:\mydocs\*my string*”

You can use all the standard wild card syntax such as the following to search only .html files.

dir /s /b “c:\mydocs\*my string*.html”

In case you are wondering the /s is for recursive and the /b is to return just the path\filename (no file info such as date, size, etc).

No comments: