Windows API one-day training (58) FindFirstFile and FindNextFile Functions

Source: Internet
Author: User
During software development, you often need to maintain the data in the directory. For example, in the credit card system of the Development Bank, because many logs are created every day, a large hard disk occupies a lot of space six months later. At this time, there is a need to regularly delete all LOG data over 6 months. To implement this function, You have to traverse the entire directory and delete the files whose names and file creation time have exceeded six months. Therefore, you need to use the following API functions FindFirstFile and FindNextFile to implement this function. The FindFirstFile function is to find the first file or directory in the directory, the FindNextFile function is used to find the next file or directory. The FindFirstFile, FindNextFile, and FindClose functions are declared as follows, _ out LPWIN32_FIND_DATAW lpFindFileData); # ifdef UNICODE # define FindFirstFile FindFirstFileW # else # define FindFirstFile FindFirstFileA # endif //! UNICODE encode (_ in HANDLE hFindFile, _ out LPWIN32_FIND_DATAA lpFindFileData); encode (_ in HANDLE hFindFile, _ out LPWIN32_FIND_DATAW lpFindFileData ); # ifdef UNICODE # define FindNextFile FindNextFileW # else # define FindNextFile FindNextFileA # endif //! UNICODE WINBASEAPIBOOLWINAPIFindClose (_ inout HANDLE hFindFile ); LpFileNameIs the directory name. Wildcard characters are generally used. LpFindFileDataIs the file or directory property found. HFindFileIs the handle of the next file or directory. An example of calling a function is as follows: #001 # pragma once #002 #003 // #004 // traverses a directory file. #005 // Cai junsheng 2007/10/25 QQ: 9073204 Shenzhen #006 // #007 class CFindFile #008 {#009 public: #010 #011 CFindFile (void) #012 {#013 m_hFind = INVALID_HANDLE_VALUE; #014 m_bFound = false; #015 memset (& m_FindFileData, 0, sizeof (m_FindFileData); #016} #017 #018 ~ CFindFile (void) #019 {#020 if (m_hFind! = INVALID_HANDLE_VALUE) #021 { #022: FindClose (m_hFind );#023} #024} #025 #026 // find the first file. #027 void First (LPCTSTR lpFileName) #028 { #029 m_hFind =: FindFirstFile (lpFileName, & m_FindFileData );#030 if (m_hFind! = INVALID_HANDLE_VALUE) #031 {#032 m_bFound = true; #033} #034 else #035 {#036 m_bFound = false; #037} #038} #039 #040 // find the file. #041 void Next (void) #042 { #043 m_bFound = FindNextFile (m_hFind, & m_FindFileData )? True: false;#044 }# 045 #046 // you can find the file. #047 bool IsOK (void) const #048 {#049 return m_bFound; #050} #051 #052 // return the attributes of the current file. #053 const WIN32_FIND_DATA & GetCurFile (void) #054 {#055 return m_FindFileData; #056} #057 #058 protected: #059 HANDLE m_hFind; // Save the location HANDLE to be searched. #060 bool m_bFound; // whether the current search is successful. #061 WIN32_FIND_DATA m_FindFileData; // Save the attributes of the current file. #062 #063}; # Use 064 as follows: #001 // search for a file. #002 CFindFile findDemo; #003 for (findDemo. first (_ T (". \ * "); #004 findDemo. isOK (); #005 findDemo. next () #006 {#007 // #008 OutputDebugString (findDemo. getCurFile (). cFileName); #009 OutputDebugString (_ T ("\ r \ n"); #010} #011
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.