Click here to Skip to main content
11,928,569 members (49,978 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

Multi-threaded file finder class

, 29 Mar 2002
Rate this:
Please Sign up or sign in to vote.
A class that searches a directory tree for files using a worker thread, keeping the main app. usable.


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;
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:



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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralSuggestion Pin
Niklas Lindquist22-Jun-09 9:31
memberNiklas Lindquist22-Jun-09 9:31 
GeneralMulti-threaded DLL Pin
JRaiden29-Jan-06 14:39
memberJRaiden29-Jan-06 14:39 
GeneralAnother at CodeProject Pin
Daniel Madden1-Apr-02 20:00
memberDaniel Madden1-Apr-02 20:00 
GeneralOne minor point Pin
Tim Smith31-Mar-02 3:48
memberTim Smith31-Mar-02 3:48 
GeneralRe: One minor point Pin
sultan_of_6string31-Mar-02 6:27
membersultan_of_6string31-Mar-02 6:27 
GeneralRe: One minor point Pin
Todd Smith31-Mar-02 8:29
memberTodd Smith31-Mar-02 8:29 
GeneralRe: One minor point Pin
sultan_of_6string31-Mar-02 8:58
membersultan_of_6string31-Mar-02 8: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 1:15
memberJase Jennings5-Apr-02 1: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 30 Mar 2002
Article Copyright 2002 by sultan_of_6string
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid