2007-10-03
| Rate This Article: | Add This Article To: |
It happens to me every so often. I need to look through a directory and find files that have specific text in them. Easy you might say, just use the proper file mask (such as t*.bmp or tr??.txt) when searching. That works in many cases, but only when the specific text you’re looking for is the start of the file name.
Let’s say for instance you want to find all files with “chocolate” in them. If you use the search string “chocolate*.*” then you will find any file that begins with “chocolate”. But you’ll miss files that contain “chocolate” yet don’t start with “chocolate”. Examples of this might be files named “I Love Chocolate.doc” and “We Eat Chocolate.txt”.
This article offers a solution. It’s built on the new addition to .NET known as LINQ (Language INtegrated Query). The idea came to me after I read Jeff Cogswell’s
Obtaining the Files in a Directory
The first thing we need to do is talk about how to get files from a directory. It’s actually trivial with the Directory object that’s contained in the System.IO namespace. The following code gets all files in the “c:\MyData\SomeStuff” directory.
string[] FileList = Directory.GetFiles(@"c:\MyData\SomeStuff" );The Directory.GetFiles method also supports wildcards, too. The following code gets all of the JPEG files from the same directory.
string[] FileList = Directory.GetFiles(@"c:\MyData\SomeStuff","*.jpg");Once again, though, using wildcards doesn’t allow us to find strings that are contained within the file name.
I’d like to point out that we could actually create some code that finds a text string within a file name, and I have done this before. But no matter how hard I tried, the solution wasn’t very eloquent, and as programmers we always strive for eloquence.
Using LINQ to Match Patterns in a List of Files
We can use LINQ to easily find patterns in a list of file names, even if the patterns are at the beginning of the file name. The following code will find the word “chocolate” in file names, regardless of where in the file name it occurs.
var MyList = from f in Directory.GetFiles((@"c:\MyData\SomeStuff")
where f.Contains("chocolate")
select f;
One of the issues with this code, though, is that it does a case sensitive search. A simple addition to the code gives you a case insensitive search as follows.
var MyList = from f in Directory.GetFiles((@"c:\MyData\SomeStuff")
where f.ToUpper().Contains("CHOCOLATE")
select f;
And let’s say that you’re using a textbox named txtSearchString to allow users to enter in a search string, you could create a case insensitive search as follows.
var MyList = from f in Directory.GetFiles((@"c:\MyData\SomeStuff")
where f.ToUpper().Contains(txtSearchString.Text.ToUpper())
select f;
You need to know that these directory searches match text in the entire file path, including the directory structure. For instance, if you’re searching for “george" then both of the file paths “c:\HereAndThere\George.doc” and “c:\Data\George\test.doc” will be part of the resulting recordset.
We can easily alter our LINQ code so that only the file name (and not the path) will be searched for a match. The following code does a case sensitive search on just the file name.
var MyList = from f in Directory.GetFiles((@"c:\MyData\SomeStuff")
where Path.GetFilename(f).Contains(txtSearchString.Text)
select f;
And the last permutation of these directory searches would be one that does a case insensitive search of just the file name. That code can be seen below.
var MyList = from f in Directory.GetFiles((@"c:\MyData\SomeStuff") where Path.GetFilename(f).ToUpper().Contains(txtSearch.Text.ToUpper()) select f;
The Demo Program
I wrote a demo program named LINQDirectorySearchDemo that can be
The program has two options. You can select a checkbox to determine if the search is case sensitive. You can also select a checkbox to determine if the search includes the full file name and path, or just the file name.
Conclusion
LINQ makes a number of tasks easier. And since this is one task I needed an eloquent solution for, I’m really glad for LINQ.
![]() |
|


