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.
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
bool recursive; bool returnFolders;
string excludeFile; string excludeDir; };
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
Here is an example of a program that uses
opts.excludeDir = "CSE*;*53*";
opts.excludeFile = "*.pdf;*.doc";
opts.filter = "*";
opts.location = "C:\\School";
opts.recursive = true;
opts.returnFolders = false;
opts.terminateValue = NULL;
int nfiles = (int) find.filelist.size();
__int64 size = find.listsize;
for (int i = 0; i < (int) find.filelist.size(); i++)
string fullname = FindFile::combinePath(find.filelist[i].path,
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".
- 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.
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.