[Question] How to delete a non-empty directory, and all the contents in its directory:
[Solution 1] If recursive deletion is not performed. You can use the API function SHFileOperation, which can delete the directory and the subdirectory and files below.
Sample code:
Bool Deltree (LPCTSTR LPSZPATH)
{
SHFileOpstruct fileop;
Fileop.fflags = FOF_NOCONFIMATION;
Fileop.hnamemappings = null;
Fileop.hwnd = NULL;
Fileop.lpszprogressTitle = null;
Fileop.pfrom = lpszpath;
Fileop.pto = NULL;
Fileop.wfunc = fo_delete;
Return SHFILEOPERATION (& Fileop) == 0;
}
[Solution 2] Use recursive calls to delete one by one:
Sample code:
Bool deletedirectory // If you delete deletedirectory ("c: // aaa")
{
CfileFind TempFind;
CHAR TEMPFILEFIND [MAX_PATH];
Sprintf (TempFileFind, "% S //*.*", DIRNAME);
Bool isfinded = (bool) tempfind.FindFile (TempFileFind);
While (isfinded)
{
IsFinded = (BOOL) TempFind.FindNextFile ();
IF (! tempfind.isdots ())
{
Char FoundFileName [MAX_PATH];
STRCPY (FoundFileName, Tempfind.getFileName (). getBuffer (MAX_PATH));
IF (Tempfind.Indirectory ())
{
CHAR TEMPDIR [MAX_PATH];
Sprintf (Tempdir, "% S / /% S", DIRNAME, FOUNDFILENAME);
DELETEDIRECTORY (TEMPDIR);
}
Else
{
Char TempFileName [MAX_PATH];
Sprintf (TempFileName, "% S / /% S", DIRNAME, FOUNDFILENAME
Deletefile (TempFileName);
}
}
}
Tempfind.close ();
IF (! RemoveDirectory (DIRNAME))
{
MessageBox (0, "Delete Directory Failed!", "Warning Information", MB_OK); // If you do not find a folder, delete fails, you can delete this sentence
Return False;
}
Return True;
}