File Search Class with control over traversal depth

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.