CFileFinder - Extend the functionality of CFileFind MFC class






4.89/5 (45 votes)
Aug 6, 2002
4 min read

373795

6149
Search for files by name, size, date, text contents. The search can be performed in a single folder or including it's subfolders.
Introduction
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
FindFiles
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 ?)
Find
int Find(CFileFinder::CFindOpts &opts)
Find files matching the conditions established in the CFindOpts
class parameter (See explanation of
CFindOpts
below)
FindTextInFile
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.
GetFileCount
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.
GetFilePath
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.
FindPathItem
int FindPathItem(LPCTSTR szPath)
Return the index of the file whose path matches szPath
or -1 if the file is not in the list.
RemoveAt
void RemoveAt(int nIndex)
Remove item at nIndex
position.
RemoveAll
void RemoveAll()
Remove all items from the list.
SetCallback
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 callingCFileFinder
objectdwCode
.- The calling reason:FF_FOUND
- A new file was foundFF_DISCARDED
- A file matched the file mask but not all the conditionsFF_FOLDER
- The search in a new folder startedFF_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 functionSetCallback
.
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);
StopSearch
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.
GetSearchingFolder
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 maskFIND_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 thedwFileAttributes
member (this member can take the same values as the one with the same name inWIN32_FIND_DATA
struct)FIND_TEXT
- The file has to contain the text specified insFindText
sBaseFolder
- The search root foldersFileMask
- 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 flagFIND_TEXT
must be added to dwOptionsFlags. IfsFindText
is empty the text search is not performed even if theFIND_TEXT
is setbSubfolders
- A boolean indicating whether the search must include subdirectories or notnMinSize
/nMinSize
- The file size range when theFIND_SIZE
flag is settMinCreated
/tMaxCreated
- The file creation date range when theFIND_DATECREATED
flag is settMinModified
/tMaxModified
- The file last modified range when theFIND_DATEMODIFIED
flag is settMinAccessed
/tMaxAccessed
- The file last accessed range when theFIND_DATEACCESSED
flag is setdwFileAttributes
- The file attributes when theFIND_ATTRIBUTES
is set
Class methods
void Reset()
- empty
sBaseFolder
- set
sFileMask
to "*.*" - set
bSubFolders
toFALSE
- set
nMinSize
,nMaxSize
to 0 - reset
dwFileAttributes
anddwOptionsFlags
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
todwOptionsFlags
void FindAllFiles()
- add all files attributes to the search, even hidden files, system files.
- add
FIND_ATTRIBUTES
todwOptionsFlags
void FindDirectories()
- add directories to the search (directories have an own attribute:
FILE_ATTRIBUTE_DIRECTORY
) - add
FIND_ATTRIBUTES
todwOptionsFlags
void FindText(LPCTSTR szText)
- set
sFindText
toszText
- add
FIND_TEXT
todwOptionsFlags
Notes
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