Delete two implementations that are not empty directory

xiaoxiao2021-03-06  59

It is well known that the process of deleting a directory that is not empty is actually a function recursive call. It is nothing more than to call the cfilefile of the MFC and the FindNextFile or the FindNextFile function, just use the CFileFind class. The implementation process is relatively simple than the implementation of the API. By following two ways, the deletion is not empty.

// Use the CFileFind class in the MFC to delete the directory BOOL __STDCALL DELETIRECTORE (CFILEFIND TEMPFIND; char TempFileFind; char tempfilefind; cHAR TempFileFind; char tempfilefind; sprintf (tempfilefind, "% s //*.*", PSZDIR) ;

Bool isfinded = reinterpret_cast (TempFind.FindFile (TempFileFind));

While (isfinded) {// The reinterpret_cast mandatory type conversion here is to convert the BOOL type to BOOL type isfinded = reinterpret_cast ()); if (! TempFind.isdots ()) {Char FoundFileName [ MAX_PATH]; STRCPY (FoundFileName, TempFind.getFileName (). GetBuffer (TempFind.Indirectory ()) {If it is a directory, the recursive call function itself char Tempdir [max_path]; sprintf (Tempdir, "% s //% s ", pszdir, findfilename); deletedIRectory (Tempdir);} else {// If it is a file, delete this file char TempFileName [max_path]; sprintf (TempFileName,"% s //% s ", pszdir, FoundFileName DELETEFILE (TEMPFILENAME);}}} tempfind.close ();

IF (! RemoveDirectory) // Delete the directory Return False;

Return True;}

// Use the API mode to delete the directory Bool deletedirectory (const char * pszdir) {Win32_find_data fd; char sztempfilefind [MAX_PATH] = {0}; Bool Bisfinish = FALSE

ZeromeMory (& fd, sizeof (win32_find_data));

Sprintf (sztempfilefind, "% s //*.*", pszdir);

Handle Hfind = FindfirstFile (sztempfilefind, & fd); if (hfind == invalid_handle_value) Return False;

While (! bisfinish) {bisfinish = (FindNextFile (HFIND, & FD))? false: true; if ((Fd.cfileName, ")! = 0 && (Strcmp (fd.cfilename," .. " !) = 0)) {char szFoundFileName [MAX_PATH] = {0}; strcpy (szFoundFileName, fd.cFileName); if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {char szTempDir [MAX_PATH] = {0}; sprintf (szTempDir, " % s //% s ", pszDir, szFoundFileName); deleteDirectory (szTempDir);} else {char szTempFileName [MAX_PATH] = {0}; sprintf (szTempFileName,"% s //% s ", pszDir, szFoundFileName); DeleteFile (sztempfilename);}}} findclose (HFIND);

IF (! REMOVEDIRECTORY) RETURN FALSE; RETURN TRUE;}

The above is to delete two implementations of folders that are not empty. The basic idea is not different. It is just easy to use the MFC than directly using the API. This is not!

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

New Post(0)