March 21, 2019
Hot Topics:

Managed Extensions: Finding Files by Extension

  • September 24, 2004
  • By Tom Archer
  • Send Email »
  • More Articles »

Searching for Files

Once I had the basic demo UI in place, I wrote a very simple class (FileSearcher) that implements a single static method (FindByExtension) to allow a caller to search for files in a specified folder (and optionally, its subfolders). Here are the basic steps that accomplished the task:
  1. Call the System::IO::Directory::GetFiles method to retrieve all files for a specified folder. The files' names are returned in an ArrayList object where each file is a String object.
  2. Enumerate the returned ArrayList object using a for loop.
  3. For each String (file name) in the ArrayList, call the Path::GetExtension method to return the file name's extension and then compare it (ignoring case) to the caller-specified extension. If the strings match, then the file name is added to the caller's passed ArrayList object.
  4. Once the current folder has been enumerated, the method checks the Search-Subfolders flag. If it is off, the method returns. If it is on, the method calls the System::IO::Directory::GetDirectories method to retrieve all folders for the current folder. These folders are then enumerated and the FileSearcher::FindByExtension method calls itself for each found folder.
Here's the code from the attached demo's Form1.h file:
__gc class FileSearcher
  static void FindByExtension(String* startingFolder, 
                              String* extension, 
                              ArrayList* matchingFiles,
                              bool recurseSubFolders)
    String* fileList[] = Directory::GetFiles(startingFolder);

    String* fileName;

    // search current folder
    for (int i = 0; i < fileList->Count; i++)
      fileName = static_cast<String*>(fileList->Item[i]);

      if (0 == String::Compare(Path::GetExtension(fileName), 
                                              extension->StartsWith(S".") ? S"" : S".", 

    if (recurseSubFolders)
      // for each subfolder
      fileList = Directory::GetDirectories(startingFolder);
      for (int i = 0; i < fileList->Count; i++)
The client then uses the FileSearcher class like this (where the value being passed to the FindByExtension method is being retrieved from the form):
ArrayList* matches = new ArrayList();


for (int i = 0; i < matches->Count; i++)

Areas That Can Be Improved

While this article has illustrated how to display a folder-browser dialog and how to search a user's folder(s) for files matching a specified extension, the solution has plenty of room for improvement. (Isn't there always?) Mainly, the FileSearcher class could be augmented to support multiple extensions, as well as asynchronous delegates and events to notify the caller of progress. That way, the caller isn't blocked on the call to the FindByExtension method and can display a progress bar, as well as allow the user to cancel what could be a very lengthy operation.

Not only would that flesh out the FileSearcher class, making it more usable, but it would also represent a great example of where and how asynchronous delegates and events can be used. Depending on the feedback I get from this article, I'll do that article at some time in the future.

Download the Code

To download the accompanying source code for this article, click here.

About the Author

The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date