Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralAdd Sort features Pin
ekey
1:47 1 Jul '09  
Generalanother thank you !! Pin
masotta
6:45 9 Dec '08  
GeneralTime to place a "Thank you" Pin
ubentz
6:28 21 Dec '06  
GeneralCase sensitivity in filter [modified] Pin
gugy1
0:09 14 Jun '06  
GeneralVisual C++ 2005 Express Compiler Error Pin
gary_johnson_53
7:06 8 Mar '06  
Generalno unicode Pin
Jan VO
0:13 14 Nov '05  
Generalbug in wildcard Pin
heroboy
19:02 5 Sep '05  
GeneralSource code link failure Pin
Eckman
2:27 16 Aug '05  
Generallink error Pin
densitet
3:33 3 Mar '05  
GeneralRe: link error Pin
Louka Dlagnekov
8:12 3 Mar '05  
GeneralRe: link error Pin
densitet
5:43 4 Mar '05  
Generallinking error in msvc6 with stlport 4.6.2 Pin
bektek
17:18 15 Sep '04  
GeneralRe: linking error in msvc6 with stlport 4.6.2 Pin
Louka Dlagnekov
18:12 15 Sep '04  
GeneralRe: linking error in msvc6 with stlport 4.6.2 Pin
bektek
20:43 15 Sep '04  
GeneralSearch differect hard drives with one location... Pin
CyberJay82
9:27 26 Jul '04  
GeneralRe: Search differect hard drives with one location... Pin
Louka Dlagnekov
16:29 26 Jul '04  
GeneralVS and MFC Solutions: Pin
Alan Phang SP
20:26 7 Mar '04  
GeneralRe: VS and MFC Solutions: Pin
Louka Dlagnekov
20:30 7 Mar '04  
GeneralHelp NEEDED PLEASE!! Pin
Zero_consequences
21:54 13 Jan '04  
GeneralRe: Help NEEDED PLEASE!! Pin
Louka Dlagnekov
16:53 18 Jan '04  
GeneralGerman Umlaute (and probably other special characters) Pin
ToK78
6:38 3 Jan '04  
Generaldu* matches data.txt Pin
L e P o
3:27 25 Nov '03  
GeneralRe: du* matches data.txt Pin
DirkS
20:47 18 Jan '04  
GeneralFantastic! Pin
Joe McConnell
7:55 19 Nov '03  
Generalfound a possible error...please help Pin
agd011
12:28 26 Jun '03  


Last Updated 12 Jan 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009