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

CFileFinder - Extend the functionality of CFileFind MFC class

, 15 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Search for files by name, size, date, text contents. The search can be performed in a single folder or including it's subfolders.

Sample Image - image.gif


CFileFinder uses CFileFind to perform a file search starting from a directory and including optionally it's subdirectories in the search.

  • A list of file paths is mantained internally.
  • If we make use of the callback mechanism, new found file paths can be obtained immediatly.
  • The class allows not only searches based on a file mask but also on the file size, file dates, text contents and file attributes.

Class methods


int FindFiles(LPCTSTR szBaseFolder, LPCTSTR szFileMask, BOOL bSubFolders = FALSE)

Search for files which match the mask szFileMask. This file mask can include one or more wildcard characters (* and ?)


int Find(CFileFinder::CFindOpts &opts)

Find files matching the conditions established in the CFindOpts class parameter (See explanation of CFindOpts below)


BOOL FindTextInFile(LPCTSTR szFile, LPCTSTR szText)

Return TRUE if the text szText was found in the file szFile. It's internally used by the Find method. It's declared public to allow applications make use of this functionality.


int GetFileCount()

Return the count of items found up to the moment the call is performed or the total of files found in the last Find or FindFiles operation.


CPath GetFilePath(int nIndex)

Return a CPath object with the required file index path. In the CPath class there are two useful operators defined: operator LPCTSTR () and operator = (LPCTSTR szPath) which allows, for instance, direct assign from and to a CString object.

// ...

CString str1("c:\\temp\\list.txt"), str2;
CPath path;

path = str1;
str2 = path;

// at this point, str2 contains the 
// string "c:\temp\list.txt"

For more information about the CPath class see the related article.


int FindPathItem(LPCTSTR szPath)

Return the index of the file whose path matches szPath or -1 if the file is not in the list.


void RemoveAt(int nIndex)

Remove item at nIndex position.


void RemoveAll()

Remove all items from the list.


void SetCallback(FILEFINDERPROC pFileFinderProc, void *pCustomParam)

Set the callback function pointer used by the search process from the Find method. The callback function is defined in the following way:

void FileFinderProc(CFileFinder *pFinder, 
        DWORD   dwCode, void    *pCustomParam);
  • pFinder .- The calling CFileFinder object
  • dwCode .- The calling reason:
    • FF_FOUND - A new file was found
    • FF_DISCARDED - A file matched the file mask but not all the conditions
    • FF_FOLDER - The search in a new folder started
    • FF_FINDTEXT - Searching for text in a file. This gives our applications the chance to process messages while searching text on big files.
  • pCustomParam .- The same pointer passed to the function SetCallback.

The name of the new found file, when the dwCode parameter is FF_FOUND, can be obtained with a call like this:

pFinder->GetFilePath(pFinder->GetFileCount() - 1);


void StopSearch()

If this method is called from within the FileFinderProc callback function, the find process is stopped but the list of files is not deleted.


LPCTSTR GetSearchingFolder()

Calling this method from FileFinderProc when the dwCode parameter is FF_FOLDER, the name of the folder being searched is retrieved.

The CFindOpts class

This class help us configuring the search to be carried out. Its methods are simple helpers to set the class members easily for the most common searches.

Class members explained

  • dwOptionsFlags - This flags indicate the constrains to be taken into account apart from the file mask
    • FIND_SIZE - If the size of a file that matches the file mask is not in the range [nMinSize, nMaxSize], the file is discarded.
    • FIND_DATEMODIFIED - The last modified file date must be in the range [tMinModified, tMaxModified]
    • FIND_DATECREATED - The creation file date must be in the range [tMinCreated, tMaxCreated]
    • FIND_DATEACCESSED - The last accessed file date must be in the range [tMinAccessed, tMaxAccessed]
    • FIND_ATTRIBUTES - The attributes of the file found must match the dwFileAttributes member (this member can take the same values as the one with the same name in WIN32_FIND_DATA struct)
    • FIND_TEXT - The file has to contain the text specified in sFindText
  • sBaseFolder - The search root folder
  • sFileMask - The file mask (e.g.: "*.txt", "fi?e.*")
  • sFindText - Text that must be found in a file to consider it a match. Note that the flag FIND_TEXT must be added to dwOptionsFlags. If sFindText is empty the text search is not performed even if the FIND_TEXT is set
  • bSubfolders - A boolean indicating whether the search must include subdirectories or not
  • nMinSize / nMinSize - The file size range when the FIND_SIZE flag is set
  • tMinCreated / tMaxCreated - The file creation date range when the FIND_DATECREATED flag is set
  • tMinModified / tMaxModified - The file last modified range when the FIND_DATEMODIFIED flag is set
  • tMinAccessed / tMaxAccessed - The file last accessed range when the FIND_DATEACCESSED flag is set
  • dwFileAttributes - The file attributes when the FIND_ATTRIBUTES is set

Class methods

void Reset()

  • empty sBaseFolder
  • set sFileMask to "*.*"
  • set bSubFolders to FALSE
  • set nMinSize, nMaxSize to 0
  • reset dwFileAttributes and dwOptionsFlags flags
  • set all date members to the current date and time

void FindNormalFiles()

  • add normal files (FILE_ATTRIBUTE_ARCHIVE) to the search
  • add FIND_ATTRIBUTES to dwOptionsFlags

void FindAllFiles()

  • add all files attributes to the search, even hidden files, system files.
  • add FIND_ATTRIBUTES to dwOptionsFlags

void FindDirectories()

  • add directories to the search (directories have an own attribute: FILE_ATTRIBUTE_DIRECTORY)
  • add FIND_ATTRIBUTES to dwOptionsFlags

void FindText(LPCTSTR szText)

  • set sFindText to szText
  • add FIND_TEXT to dwOptionsFlags


The CPath class requires linking with the library shlwapi.lib

Sample code can be found in the source of the sample application provided with this article


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

Samuel Gonzalo
Software Developer (Senior)
Spain Spain
I studied Telecommunication with spezialization in Sound & Image. I was always very interested in programming at university as well and that is how I earn a living.
Since some years ago, I am also succesfully involved in software architecture and design.

Comments and Discussions

QuestionGot CInvalidArgException error while finding text. PinmemberMember 1075462517-May-14 0:15 
QuestionHere is a BUG with compiling... Pinmembercyfage12-Aug-12 15:15 
AnswerRe: Here is a BUG with compiling... PinmemberSamuel Gonzalo26-Aug-12 0:35 
AnswerRe: Here is a BUG with compiling... PinmemberSamuel Gonzalo26-Aug-12 0:42 
GeneralUnicode support Pinmembermalfaro23-Aug-10 11:38 
GeneralFileFinder and Path code for VS2008 PinmemberAntoineBab2-Jan-10 23:34 
GeneralRe: FileFinder and Path code for VS2008 Pinmemberlows22-Jan-10 3:47 
Questionhow to search office word 2003 PinmemberJackJianShen2-Sep-09 23:09 
GeneralQuestion to the function "CPath CFileFinder::GetFilePath(int nIndex)" PinmemberGolden Lee2-Apr-09 17:28 
GeneralRe: Question to the function "CPath CFileFinder::GetFilePath(int nIndex)" PinmemberSamuel Gonzalo15-Jul-09 21:27 
GeneralCompiling with VS2008 PinmemberRedFraggle25-Feb-09 13:32 
GeneralChanging the access date Pinmemberanitaj7-Jul-08 21:24 
GeneralTHNX PinmemberCTAPYLLIKA20-Mar-08 21:55 
QuestionInheritance ? Pinmember./w10-Oct-07 14:21 
AnswerRe: Inheritance ? PinmemberSamuel Gonzalo19-Nov-07 12:04 
GeneralMAX_PATH Pinmembersuperagenteg3-Oct-07 1:57 
GeneralRe: MAX_PATH PinmemberSamuel Gonzalo19-Nov-07 12:28 
GeneralWarnings are appeared when I try to Rebuild the solution [modified] PinmemberNenad B23-Mar-07 5:18 
Questionhow to search through Zip folders Pinmembermaddy_sv30-Nov-06 4:48 
Generalsome extensions Pinmemberaldo hexosa5-Nov-06 16:31 
GeneralCorrection for file matching logic PinmemberSteve Mayfield11-Sep-06 13:41 
GeneralVC 7.1 compilation problems [modified] PinmemberYenidai_23-Jun-06 1:18 
General_finder.SetCallback(FileFinderProc, this); ERROR [modified] Pinmemberlocoone11-Jun-06 13:28 
Generalplease use static Callback Function! Pinmemberswqswqswqswq17-Jun-06 21:42 
please use static Callback Function!
When you declare this callback function,please add this keyword:static
QuestionDoes not work over the network? PinmemberKyp8311-Jul-05 8:23 
AnswerRe: Does not work over the network? PinmemberThe incredible machine21-Jun-07 23:25 
GeneralAdding wildcard directory matching PinmemberSteve Mayfield4-Feb-05 22:13 
QuestionRe: Adding wildcard directory matching Pinmemberdipali_be200325-Aug-06 21:53 
GeneralLogic Error found PinmemberThomas de Rossi5-Nov-04 9:22 
GeneralNice Article PinmemberVivekAnna29-Jun-04 21:44 
GeneralDiscrepancy found Pinmemberoksale14-Apr-04 22:56 
GeneralRe: Discrepancy found Pinmemberoksale15-Apr-04 23:04 
Generala little problem PinmemberFoerst30-Mar-04 10:25 
GeneralRe: a little problem PinmemberSamuel Gonzalo30-Mar-04 20:32 
GeneralCORRECTION FOR SEARCH FILES IN CDROM Pinmemberikwi19-Aug-03 12:22 
GeneralSupport on the NT 4 SP6A PinsussAnonymous12-Aug-03 23:25 
GeneralRe: Support on the NT 4 SP6A PinmemberAnthony_Yio13-Aug-03 0:08 
GeneralRe: Support on the NT 4 SP6A Pinmemberavistav24-Sep-03 3:22 
GeneralRe: Support on the NT 4 SP6A PinmemberAnthony_Yio24-Sep-03 17:30 
GeneralRe: Support on the NT 4 SP6A Pinmemberavistav24-Sep-03 20:52 
GeneralRe: Support on the NT 4 SP6A PinmemberNicholas Wourms25-Jul-05 18:46 
GeneralVery usefull but I changed this.. Pinmemberjhaga7-Apr-03 10:39 
QuestionWay to take advantage of Microsoft's low-level search functionality? Pinmemberame1219-Jan-03 8:38 
AnswerRe: Way to take advantage of Microsoft's low-level search functionality? Pinmemberpeterchen19-May-05 11:10 
GeneralVery nice but a small problem PinmemberNyrup19-Dec-02 20:45 
Generalhelp with supporting image thumbnails PinmemberOtyug31-Oct-02 19:28 
GeneralNice tool PinmemberDerMeister16-Aug-02 4:21 
GeneralUpdate 16th August PinsussSamuel Gonzalo16-Aug-02 0:14 
GeneralLittle bug in Path.cpp PinsussAnonymous7-Aug-02 22:24 
GeneralRe: Little bug in Path.cpp PinsussSamuel Gonzalo8-Aug-02 22:21 

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 | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 16 Aug 2002
Article Copyright 2002 by Samuel Gonzalo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid