Recutory search directory with cfilefind

zhaozj2021-02-17  51

We know that cfilefind does not provide features that directly travers its subdirectories, and sometimes we often traverse all files and their subdirectories in a directory. As we have to delete a directory, and there is a subdirectory in this directory because Windows does not allow deletion of non-empty directories, so we must be able to traverse all subdirectories in a directory, which can be implemented by simple recursive.

Let's start with a simple example: How to delete a directory? (Suppose we complete this feature through the DELETEDIRECTORY (LPCTSTR DIRNAME) function)

To delete a directory, we have to complete the following steps:

1. Delete all files in this directory

2. If there is a subdirectory in this directory We want to recursively call the Deledirectory (LPCTSTR DIRNAME "function to delete all files in the subdirectory

3. Call RemoveDirectory (LPCTSTR LPPATHNAME) Delete this directory

The complete implementation of the DELETEDIRECTORY function is as follows: BOOL DELETEDIRECTORY (LPCTSTR DIRNAME) {cfileFind TempFind; file: // Declare a cfileFind class variable for searching for Char TempFileFind [200]; file: // Used to define search formats Sprintf (TempFileFind, "% s //*.*"; file: // match format is *. *, all files in this directory BOOL ISFINDED = TempFind.FindFile (TempFileFind); File: // Find the first file while (isfinded) {isfinded = (bool) tempfind.findNextFile (); file: // Removing Search Other files IF (! TempFind.isdots ()) file: // If not "." Directory {Char FoundFileName [200]; STRCPY (FoundFileName, Tempfind.getFileName (). GetBuffer (200)); if (TempFind.Indirectory ()) file: // If it is a directory, recursively call {file: // deletedIRectoryChar Tempdir [200]; Sprintf (Tempdir, "% s //% s", dirName, FoundFileName; deletedIRectory (Tempdir);} else {file: // If it is a file, Direct Direct CHAR TEMPFILENAME [200]; Sprintf TempFileName, "% s //% s", Dirname, FoundFileName); deletefile (TempFileName);}}} tempfind.close (); if (! RemoveDirectory (DIRNAME)) FILE: // Delete Directory {AFXMESSAGEBOX ("Delete Directory Failure! ", Mb_ok); return false;} return true;}

转载请注明原文地址:https://www.9cbs.com/read-30592.html

New Post(0)