View plaincopyprint? // Define # ifndef SearchFilesUnitH # define SearchFilesUnitH // ----------------------------------------------------------------------------- # include <windows. h> # include <stdio. h> typedef void (_ stdcall * PSEARCHCALLBACK) (LPCTSTR); typedef void (_ stdcall * PDIRCOMPLETEDCALLBACK) (LPCTSTR); class CSearchFile {public: // No deep initialization constructor. Degree unrestricted CSearchFile () {m_depthLevel =-1;} // scan the depth initialization constructor CSearchFile (int depthLevel) {m_depthLevel = depthLevel;} private: int m_depthLevel; private: BOOL DirectoryExist (LPCTSTR lpszDir) {DWORD dwAttribute =: GetFileAttributes (lpszDir); if (dwAttribute! = 0 xFFFFFFFF) {return (dwAttribute & FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY);} return FALSE;} public: BOOL SearchFile (LPCTSTR root/* root directory of the search file */, PSEARCHCALLBACK pFoundCallback/* after the file is found, the Callback can implement file type filtering, file deletion, copying, and other file-related operations in this Callback */, PDIRCOMPLETEDCALLBACK pDirCompletedCallback/* after a sub-directory is completed, the Callback can be used in this Callback to delete the folder */, int depthLevel = 0 );}; # Endif. cpp file [cpp] view plaincopyprint? // ----------------------------------------------------------------------------- # Include "SearchFilesUnit. h "// --------------------------------------------------------------------------- BOOL CSearchFile: SearchFile (LPCTSTR root/* root directory of the search file */, PSEARCHCALLBACK pFoundCallback/* after the file is found, the Callback can implement file type filtering, file deletion, copying, and other file-related operations in this Callback */, PDIRCOMPLETEDCALLBACK pDirCompletedCallback/* C Allback can work with the file deletion function of the previous Callback to delete folders */, int depthLevel) {char szPath [MAX_PATH] = {0}; WIN32_FIND_DATA stWFD; memset (& stWFD, 0, sizeof (WIN32_FIND_DATA); sprintf (szPath, "% s \ *", root ); // if there is a deep scan control if (m_depthLevel> 0) {if (depthLevel ++> m_depthLevel) {depthLevel = 0; return FALSE ;}} HANDLE file = :: findFirstFile (szPath, & stWFD); if (INVALID_HANDLE_VALUE = file) return FALSE; Do {if (strcmp (stWFD. cFileName ,". ") & strcmp (stWFD. cFileName ,".. ") {char szFoundFile [MAX_PATH] = {0}; sprintf (szFoundFile," % s \ % s ", root, stWFD. cFileName); // determine whether it is a directory or a file if (DirectoryExist (szFoundFile) {SearchFile (szFoundFile, pFoundCallback, pDirCompletedCallback, depthLevel);} if (NULL! = PFoundCallback) {pCallback (szFoundFile) ;}}while (: FindNextFile (file, & stWFD);: FindClose (file); if (NULL! = PDirCompletedCallback) {pDirCompletedCallback (root);} return TRUE ;}