Click here to Skip to main content
11,437,713 members (27,902 online)
Click here to Skip to main content

FindFile class for Windows projects

, 12 Jan 2004
Rate this:
Please Sign up or sign in to vote.
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

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:

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:

    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

  • December 27, 2003
    • Updated source code to reflect bug fix on searching of files in root drives.
  • November 29, 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.
  • November 28, 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

Share

About the Author

Louka Dlagnekov

United States United States
No Biography provided

Comments and Discussions

 
GeneralAdd Sort features Pin
ekey1-Jul-09 1:47
memberekey1-Jul-09 1:47 
Hi Louka,
Thank you post this aritical, it very useful for me, I added sort files by created time function, and post the code here, maybe you can merge the code into your source code:
FindFile.h:
enum OrderType
{
     None,
     CreateTimeAsc,
     CreateTimeDesc
};
// Specifies settings to use for searching for files
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

      OrderType orderType;      // FileList order type
};

FindFile.cpp:
struct AscendingFileCreateTimeSort
{
     bool operator()(const FileInformation &rpStart,const FileInformation &rpEnd)
      {
          return CompareFileTime(&rpStart.fileinfo.ftCreationTime ,&rpEnd.fileinfo.ftCreationTime) < 0;
      }
};

struct DescendingFileCreateTimeSort
{
     bool operator()(const FileInformation&amp; rpStart,const FileInformation&amp; rpEnd)
      {
          return CompareFileTime(&rpStart.fileinfo.ftCreationTime ,&rpEnd.fileinfo.ftCreationTime) > 0;
      }
};

void FindFile::search ()
{
      clear();
      scanPath (m_opts.location);
     switch(m_opts.orderType)
     {
     case OrderType::CreateTimeAsc:
          sort(filelist.begin(),filelist.end(),AscendingFileCreateTimeSort());            
                        break;
     case OrderType::CreateTimeDesc:
          sort(filelist.begin(),filelist.end(),DescendingFileCreateTimeSort());
          break;
     default:
          break;
     }
}

Best wishes,
Yun
Generalanother thank you !! Pin
masotta9-Dec-08 6:45
membermasotta9-Dec-08 6:45 
GeneralTime to place a "Thank you" Pin
ubentz21-Dec-06 6:28
memberubentz21-Dec-06 6:28 
GeneralCase sensitivity in filter [modified] Pin
gugy114-Jun-06 0:09
membergugy114-Jun-06 0:09 
GeneralVisual C++ 2005 Express Compiler Error Pin
gary_johnson_538-Mar-06 7:06
membergary_johnson_538-Mar-06 7:06 
Generalno unicode Pin
Jan VO14-Nov-05 0:13
memberJan VO14-Nov-05 0:13 
Generalbug in wildcard Pin
heroboy5-Sep-05 19:02
memberheroboy5-Sep-05 19:02 
GeneralSource code link failure Pin
Eckman16-Aug-05 2:27
memberEckman16-Aug-05 2:27 
Generallink error Pin
densitet3-Mar-05 3:33
memberdensitet3-Mar-05 3:33 
GeneralRe: link error Pin
Louka Dlagnekov3-Mar-05 8:12
memberLouka Dlagnekov3-Mar-05 8:12 
GeneralRe: link error Pin
densitet4-Mar-05 5:43
memberdensitet4-Mar-05 5:43 
Generallinking error in msvc6 with stlport 4.6.2 Pin
bektek15-Sep-04 17:18
memberbektek15-Sep-04 17:18 
GeneralRe: linking error in msvc6 with stlport 4.6.2 Pin
Louka Dlagnekov15-Sep-04 18:12
memberLouka Dlagnekov15-Sep-04 18:12 
GeneralRe: linking error in msvc6 with stlport 4.6.2 Pin
bektek15-Sep-04 20:43
memberbektek15-Sep-04 20:43 
GeneralSearch differect hard drives with one location... Pin
CyberJay8226-Jul-04 9:27
memberCyberJay8226-Jul-04 9:27 
GeneralRe: Search differect hard drives with one location... Pin
Louka Dlagnekov26-Jul-04 16:29
memberLouka Dlagnekov26-Jul-04 16:29 
GeneralVS and MFC Solutions: Pin
Alan Phang SP7-Mar-04 20:26
memberAlan Phang SP7-Mar-04 20:26 
GeneralRe: VS and MFC Solutions: Pin
Louka Dlagnekov7-Mar-04 20:30
memberLouka Dlagnekov7-Mar-04 20:30 
GeneralHelp NEEDED PLEASE!! Pin
Zero_consequences13-Jan-04 21:54
memberZero_consequences13-Jan-04 21:54 
GeneralRe: Help NEEDED PLEASE!! Pin
Louka Dlagnekov18-Jan-04 16:53
memberLouka Dlagnekov18-Jan-04 16:53 
GeneralGerman Umlaute (and probably other special characters) Pin
ToK783-Jan-04 6:38
memberToK783-Jan-04 6:38 
Generaldu* matches data.txt Pin
L e P o25-Nov-03 3:27
memberL e P o25-Nov-03 3:27 
GeneralRe: du* matches data.txt Pin
DirkS18-Jan-04 20:47
memberDirkS18-Jan-04 20:47 
GeneralFantastic! Pin
Joe McConnell19-Nov-03 7:55
memberJoe McConnell19-Nov-03 7:55 
Generalfound a possible error...please help Pin
agd01126-Jun-03 12:28
memberagd01126-Jun-03 12:28 
GeneralRe: found a possible error...please help Pin
Louka Dlagnekov26-Jun-03 12:46
memberLouka Dlagnekov26-Jun-03 12:46 
GeneralRe: found a possible error...please help Pin
agd01126-Jun-03 13:58
memberagd01126-Jun-03 13:58 
GeneralRe: found a possible error...please help Pin
agd01126-Jun-03 14:03
memberagd01126-Jun-03 14:03 
GeneralRe: found a possible error...please help Pin
Louka Dlagnekov26-Jun-03 17:47
memberLouka Dlagnekov26-Jun-03 17:47 
GeneralRe: found a possible error...please help Pin
agd01127-Jun-03 11:12
memberagd01127-Jun-03 11:12 
GeneralRe: found a possible error...please help Pin
p 5 y C 0 d E r6-Aug-03 23:46
memberp 5 y C 0 d E r6-Aug-03 23:46 
GeneralRe: found a possible error...please help Pin
agd0117-Aug-03 11:37
memberagd0117-Aug-03 11:37 
GeneralUnsuspected end of file found while looking for precompiled header!! Pin
BarçaBoy21-May-03 4:47
memberBarçaBoy21-May-03 4:47 
GeneralRe: Unsuspected end of file found while looking for precompiled header!! Pin
Anonymous21-May-03 7:17
sussAnonymous21-May-03 7:17 
GeneralRe: Unsuspected end of file found while looking for precompiled header!! Pin
BarçaBoy22-May-03 5:55
memberBarçaBoy22-May-03 5:55 
GeneralMultiple File Selecttion Pin
alpu6-Mar-03 18:28
memberalpu6-Mar-03 18:28 
GeneralRe: Multiple File Selecttion Pin
Louka Dlagnekov6-Mar-03 20:53
memberLouka Dlagnekov6-Mar-03 20:53 
GeneralVery useful. Thanks Pin
lichunan4-Feb-03 13:17
memberlichunan4-Feb-03 13:17 
GeneralRe: Very useful. Thanks Pin
Louka Dlagnekov4-Feb-03 17:17
memberLouka Dlagnekov4-Feb-03 17:17 
GeneralSearch file is error on root directory Pin
zzj8-Dec-02 15:17
memberzzj8-Dec-02 15:17 
GeneralRe: Search file is error on root directory Pin
Louka Dlagnekov9-Dec-02 16:34
memberLouka Dlagnekov9-Dec-02 16:34 
GeneralRe: Search file is error on root directory Pin
zzj9-Dec-02 22:18
memberzzj9-Dec-02 22:18 
GeneralRe: Search file is error on root directory Pin
Mr. X14-Feb-03 4:24
sussMr. X14-Feb-03 4:24 
GeneralRe: Search file is error on root directory Pin
Louka Dlagnekov23-Feb-03 16:48
memberLouka Dlagnekov23-Feb-03 16:48 
QuestionLGPL? Pin
Joshua Jensen28-Nov-02 23:55
memberJoshua Jensen28-Nov-02 23:55 
AnswerRe: LGPL? Pin
Louka Dlagnekov29-Nov-02 10:46
memberLouka Dlagnekov29-Nov-02 10:46 
GeneralRe: LGPL? Pin
Philippe Lhoste2-Dec-02 23:44
memberPhilippe Lhoste2-Dec-02 23:44 
GeneralRe: LGPL? Pin
Louka Dlagnekov4-Dec-02 18:12
memberLouka Dlagnekov4-Dec-02 18:12 

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 | Terms of Use | Mobile
Web04 | 2.8.150506.1 | Last Updated 13 Jan 2004
Article Copyright 2002 by Louka Dlagnekov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid