Click here to Skip to main content
15,879,474 members
Articles / Desktop Programming / MFC

FindFile Class for Windows Projects

Rate me:
Please Sign up or sign in to vote.
4.80/5 (27 votes)
12 Jan 20042 min read 175K   2.4K   48   50
This class is intended to alleviate the task of finding files and folders in a directory with options such as search filters; exclude file filters, exclude directory filters
I decided to write my own code to search for files and directories, make it IDE independent and add some useful features. The result of my efforts is FindFile, the details of which have been given in this article.

Introduction

Searching for files and directories is something we, as programmers, have to do in many projects. I have always thought that there must be something someone out there wrote to make my life easier in this regard. I have done a lot of looking around but most of what I've found has had a dependency on a specific IDE, like VS and MFC for example. So, I decided to write my own, make it IDE independent and add some useful features. FindFile is the result of my efforts.

Using the Code

The code that allows searching consists of the files FindFile.h, FindFile.cpp, wildcard.h, and wildcard.cpp. To use the code, you need to initialize a FindFileOptions_t structure and pass it by reference to the constructor of FindFile. Here is a snippet of the structure, listing options used by FindFile:

C++
struct FindFileOptions_t
{
    bool recursive;         // Whether to look inside subdirectories
    bool returnFolders;     // Return folder names as results too

    bool *terminateValue;   // Value to check to see whether search 
                            // should be terminated

    string location;        // Where to search for files

    string filter;          // Filter for files to be included

    string excludeFile;     // Exclude filter for files
    string excludeDir;      // Exclude filter for directories
};

Some of these options require explaining. The returnFolders option specifies whether FindFile should return both files and directories in its return results. The terminateValue option is a pointer to some integer in memory whose value will be read to determine whether the search should be aborted. This is useful to have when incorporating this class in a multi-threaded environment (as is most likely the case). If the value in the integer pointed to by terminateValue is anything but 0, the search will abort. If you do not want to use this feature, just set terminateValue to NULL.

Here is an example of a program that uses FindFile:

C++
FindFileOptions_t opts;
opts.excludeDir = "CSE*;*53*";
opts.excludeFile = "*.pdf;*.doc";
opts.filter = "*";
opts.location = "C:\\School";
opts.recursive = true;
opts.returnFolders = false;
opts.terminateValue = NULL;

FindFile find(opts);
find.search();

int nfiles = (int) find.filelist.size();
__int64 size = find.listsize;

// Print out the list of files found
for (int i = 0; i < (int) find.filelist.size(); i++)
{
    string fullname = FindFile::combinePath(find.filelist[i].path,
                   find.filelist[i].fileinfo.cFileName);
    printf("%s\n", fullname.c_str());
}

printf("Found: %d files (%ld bytes)", nfiles, size);

This example will recursively search for all files in directory "C:\School" which do not have a PDF or DOC extension and will not look for files in subdirectories that begin with "CSE" or contain "53".

History

  • 27th December, 2003
    • Updated source code to reflect bug fix on searching of files in root drives
  • 29th November, 2002
    • All references to the GNU public license have been removed from the code. You are free to use this code any way you see fit as long as the original copyright statement remains in all versions you modify/redistribute.
  • 28th November, 2002
    • This is the first version of the code but has been used in many projects of mine for many years. If you find a bug, though, or feel some parts can be done better, please let me know.

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.


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerUNICODE Pin
Michael Haephrati13-May-20 13:57
professionalMichael Haephrati13-May-20 13:57 
GeneralRe: UNICODE Pin
Paul Belikian15-Jul-20 14:44
Paul Belikian15-Jul-20 14:44 
GeneralAdd Sort features Pin
ekey1-Jul-09 0:47
ekey1-Jul-09 0:47 
Generalanother thank you !! Pin
masotta9-Dec-08 5:45
masotta9-Dec-08 5:45 
GeneralTime to place a "Thank you" Pin
ubentz21-Dec-06 5:28
ubentz21-Dec-06 5:28 
GeneralCase sensitivity in filter [modified] Pin
gugy113-Jun-06 23:09
gugy113-Jun-06 23:09 
GeneralVisual C++ 2005 Express Compiler Error Pin
gary_johnson_538-Mar-06 6:06
gary_johnson_538-Mar-06 6:06 
Generalno unicode Pin
Jan VO13-Nov-05 23:13
Jan VO13-Nov-05 23:13 
Generalbug in wildcard Pin
heroboy5-Sep-05 18:02
heroboy5-Sep-05 18:02 
GeneralSource code link failure Pin
Eckman16-Aug-05 1:27
Eckman16-Aug-05 1:27 
Generallink error Pin
densitet3-Mar-05 2:33
densitet3-Mar-05 2:33 
GeneralRe: link error Pin
Louka Dlagnekov3-Mar-05 7:12
Louka Dlagnekov3-Mar-05 7:12 
GeneralRe: link error Pin
densitet4-Mar-05 4:43
densitet4-Mar-05 4:43 
Generallinking error in msvc6 with stlport 4.6.2 Pin
bektek15-Sep-04 16:18
bektek15-Sep-04 16:18 
GeneralRe: linking error in msvc6 with stlport 4.6.2 Pin
Louka Dlagnekov15-Sep-04 17:12
Louka Dlagnekov15-Sep-04 17:12 
GeneralRe: linking error in msvc6 with stlport 4.6.2 Pin
bektek15-Sep-04 19:43
bektek15-Sep-04 19:43 
GeneralSearch differect hard drives with one location... Pin
CyberJay8226-Jul-04 8:27
CyberJay8226-Jul-04 8:27 
Is it possible to search all of a computers hardrives without knowing the drive letters, example my one computer has only one drive (C:\) and my desktop has two drives (C:\ and F:\). So i want to specify one location like a wildcard or something so that it will search all hardrives whether it be one or many drives?
GeneralRe: Search differect hard drives with one location... Pin
Louka Dlagnekov26-Jul-04 15:29
Louka Dlagnekov26-Jul-04 15:29 
GeneralVS and MFC Solutions: Pin
Alan Lewis7-Mar-04 19:26
Alan Lewis7-Mar-04 19:26 
GeneralRe: VS and MFC Solutions: Pin
Louka Dlagnekov7-Mar-04 19:30
Louka Dlagnekov7-Mar-04 19:30 
GeneralHelp NEEDED PLEASE!! Pin
Zero_consequences13-Jan-04 20:54
Zero_consequences13-Jan-04 20:54 
GeneralRe: Help NEEDED PLEASE!! Pin
Louka Dlagnekov18-Jan-04 15:53
Louka Dlagnekov18-Jan-04 15:53 
GeneralGerman Umlaute (and probably other special characters) Pin
ToK783-Jan-04 5:38
ToK783-Jan-04 5:38 
Generaldu* matches data.txt Pin
Member 72557725-Nov-03 2:27
Member 72557725-Nov-03 2:27 
GeneralRe: du* matches data.txt Pin
DirkS18-Jan-04 19:47
DirkS18-Jan-04 19:47 

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

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