Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / MFC
Article

Multi-threaded file finder class

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
29 Mar 20021 min read 73.4K   2.2K   37   8
A class that searches a directory tree for files using a worker thread, keeping the main app. usable.

Introduction

The CFileFinder class searches a directory tree for files and adds their full paths to a CStringList object (my own class, included with the source). It is built in 'pure Windows'--no MFC--with just a few classes of my own. The main feature of CFileFinder is that it uses a worker thread to find the files. File finding can take quite a while, so this lets the main program remains responsive to the user. By means of a callback function, the main program can continuously update a file list. Furthermore, CFileFinder can be halted at any time by calling CFileFinder::Stop().

NOTE: CFileFinder and its requisite classes use UNICODE. Porting them to ANSI for Win 95/98/Me is a no-brainer, but that's something I'm not going to bother doing. If you still use such 3rd-rate OSs (no offense) then you can go ahead and do so, or use the Microsoft Layer for Unicode that will do the job for you.

Quick Guide:

First, create the global CFileFinder and CStringList (that receives the file list) objects:

CFileFinder g_filefinder;<br>CStringList g_files;

Call CFileFinder::Start to begin the search. The function returns immediately:

g_filefinder.Start(&g_files, strRootfolder, strFilter,
    TRUE /* search sub-folders */, FinderCallback, 0);

Note that the path to the root folder must end in a backslash.

The callback function notifies the application of the last found file, whether the operation is complete (in which case there is no last found file), whether there was an error and a user specified UINT:

void CALLBACK FileFinderProgressProc(CFileFinder* pFileFinder, 
    PCWSTR pszFile, BOOL bDone, BOOL bError, UINT uUserData);

The CFileFinder::Stop function can be called at any time to halt the search:

g_finder.Stop();

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

 
GeneralSuggestion Pin
Niklas L22-Jun-09 8:31
Niklas L22-Jun-09 8:31 
GeneralMulti-threaded DLL Pin
JRaiden29-Jan-06 13:39
JRaiden29-Jan-06 13:39 
GeneralAnother at CodeProject Pin
Dan Madden1-Apr-02 19:00
Dan Madden1-Apr-02 19:00 
GeneralOne minor point Pin
Tim Smith31-Mar-02 2:48
Tim Smith31-Mar-02 2:48 
GeneralRe: One minor point Pin
sultan_of_6string31-Mar-02 5:27
sultan_of_6string31-Mar-02 5:27 
GeneralRe: One minor point Pin
Todd Smith31-Mar-02 7:29
Todd Smith31-Mar-02 7:29 
GeneralRe: One minor point Pin
sultan_of_6string31-Mar-02 7:58
sultan_of_6string31-Mar-02 7:58 
Thanks for the tip scoping the callback typedef in the class. It's a lot neater now, since the project in which I'm using CFileFinder has five other classes that use callbacks.

You're right, the CStringList and the CString classes both have a lot of 'superfluous' functions. However, I need them in my project. It would be easy to decouple them though.

Thanks for the feedback.
GeneralRe: One minor point Pin
Jase Jennings5-Apr-02 0:15
Jase Jennings5-Apr-02 0:15 

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.