65.9K
CodeProject is changing. Read more.
Home

Multi-threaded file finder class

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (3 votes)

Mar 30, 2002

1 min read

viewsIcon

74319

downloadIcon

2221

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