Click here to Skip to main content
Click here to Skip to main content

Using DirectoryInfo class to search for files in a directory on both single pattern and multiple patterns

, 7 Jun 2006
Rate this:
Please Sign up or sign in to vote.
The articles simplifies searching files in a directory with single search pattern and multiple search patterns.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Introduction

In this article, I have tried to simply understanding of using DirectoryInfo class in .NET to search for files in a specific directory. The DirectoryInfo class provides information about a given directory which can be set through the constructor while creating the DirectoryInfo object. Once we get a DirectoryInfo object bound to a directory on the file system, we can get any sort of information about it including files it contains. I have included examples of searching for specific files in the directory instead of getting the complete list. The .NET framework provides a complete search mechanism for searching filenames and file extensions. For a very simple example:

DirectoryInfo dinfo = new DirectoryInfo(completeDirPath);
/* get files from the directory as a files collection */
FileInfo[] finfo = dinfo.GetFiles(strExtension);
Here, I create a DirectoryInfo object bound to a directory given by completeDirPath and then get a list of all files in that directory which have the extension of strExtension. This extension can be any e.g. *.txt and *.gif and *.doc etc. The GetFiles(strExtension) method returns a list of type FileInfo where each FileInfo represents a file and provides detailed information about that file. Here is a limitation! The GetFiles(strExtension) method can take only one pattern as argument. This means that we can search either *.txt files or *.doc files at a time. If we want to get a list of all txt and doc files, then we will have to use a little logic to get the required lists and join them by ourselves. This happens sometime when we really need to do it. For instance, I need to get all JPEG images in my directory which can be either jpg or jpeg. Both are valid formats but the search pattern will not allow me to search for them. The solution to the problem is quite simple:
public override FileInfo[] getFiles(string ext)
{
/* Pointer to our directory */
if (dirPath == null)
    return null;

DirectoryInfo dinfo = new DirectoryInfo(dirPath);
string[] exts = ext.Split(',');
FileInfo[][] finfo = new FileInfo[exts.Length][];
int i = 0;

foreach(string e in exts)
{
    /* get files from the directory as a files collection */
    finfo[i++] = dinfo.GetFiles(e);
}

int tlength = 0;

for (i = 0 ; i < finfo.Length ; i++)
{
    tlength += finfo[i].Length;
}

FileInfo[] res = new FileInfo[tlength];
int j = 0;
int rindex = 0;

for (i = 0 ; i < finfo.Length ; i++)
{
    for (j = 0 ; j < finfo[i].Length ; j++)
    {
        res[rindex++] = finfo[i][j];
    }
}

return res;
}
This method receives a comma seperated list of extensions and searches the directory for each extension one by one. It makes a jagged array for containing lists against each file extension and them merges those arrays to make a single complete files list. I have attached a complete running example with this article. In the example, if you search more than one extensions, the second method is called and if you search for only one extension, the simple straight forward method is called. I have created an abstract base class DirectoryInfoEx and two classes SingleExt and MultipleExt inherit from it. This article is part of my .NET surfing and I soon dispatch more detailed and advanced topics based on this example. Keep reading and happy dotnetting Smile | :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

immaad
Software Developer
Australia Australia
C/C++, C# .NET, VB.NET, Linux, Oracle, PostgreSQL, MYSQL

Comments and Discussions

 
Generalthe same using LINQ PinmemberKubin23-Aug-10 2:49 
GeneralCool Pinmemberkiweed10-Oct-07 9:48 
QuestionWill it work fine for *.* ??? Pinmembernitinr70822-Aug-07 20:07 
AnswerRe: Will it work fine for *.* ??? Pinmembernitinr70822-Aug-07 20:28 
SuggestionRe: Will it work fine for *.* ??? Pinmemberphil.o2-Jul-11 7:32 
GeneralSuggestion PinmemberAlex Cater29-Nov-06 5:33 
GeneralRe: Suggestion PinmemberM Gillespie20-Apr-07 0:07 
GeneralRe: Suggestion [modified] PinmemberAlex Cater20-Apr-07 12:26 
GeneralRe: Suggestion PinmemberHosamAly17-Jan-11 5:45 
QuestionsearchPattern "*.txt" is actually "*.txt*"????? Pinmembersheir24-Jul-06 5:47 
AnswerRe: searchPattern "*.txt" is actually "*.txt*"????? Pinmembericebear_X16-Jun-11 7:42 
GeneralHi dear Pinmemberlakshithap@dsisamson.com18-Jun-06 18:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 7 Jun 2006
Article Copyright 2006 by immaad
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid