Click here to Skip to main content
Click here to Skip to main content

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.

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

Share

About the Author

sultan_of_6string

United States United States
No Biography provided

Comments and Discussions

 
GeneralSuggestion PinmemberNiklas Lindquist22-Jun-09 8:31 
GeneralMulti-threaded DLL PinmemberJRaiden29-Jan-06 13:39 
GeneralAnother at CodeProject PinmemberDaniel Madden1-Apr-02 19:00 
GeneralOne minor point PinmemberTim Smith31-Mar-02 2:48 
GeneralRe: One minor point Pinmembersultan_of_6string31-Mar-02 5:27 
GeneralRe: One minor point PinmemberTodd Smith31-Mar-02 7:29 
I noticed the error value is always hardcoded to FALSE. At the very least I would return the value of GetLastError() from the Find functions.
 
Since the only function you call on the CStringList is Add() you could decouple the dependency between CFindFile and CStringList. Then you would be free to use any kind of container to store your results.
 
You never check the value of m_pCallback. Your program will crash if someone passes in NULL.
 
If someone wanted to wait until FineFile completed (console app) they might write a loop checking InProgress(). I think it would be more efficient to allow InProgress to call WaitForSingleObject(m_hWorkerThread, INFINITE).
 
I would also decouple the thread code from the CFindFile class altogether. If you wanted to creat a CFindRegistryKey class you would have to duplicate all of the thread code.
 
I passed in c: or c:\tmp instead of c:\ or c:\tmp\ it doesn't work. It wouldn't be to hard to add a \ to the end of the input if it didn't exist.
 
I usually scope typedef's within my classes so I don't have to come up with unique names
 
class CFileFinder:
{
public:
    typedef void (CALLBACK* Callback)(CFileFinder*, PCWSTR, BOOL, BOOL, UINT);
 
    // etc.
};
 
then use CFileFinder::Callback.
 

 
Todd Smith
CPUA 0x007 ... shaken not stirred

GeneralRe: One minor point Pinmembersultan_of_6string31-Mar-02 7:58 
GeneralRe: One minor point PinmemberJase Jennings5-Apr-02 0:15 

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 | Mobile
Web04 | 2.8.140827.1 | Last Updated 30 Mar 2002
Article Copyright 2002 by sultan_of_6string
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid