Aug 202010

This is a function I use to delete all files, subfolders, and the parent folder in a tree. It’s a simple process, but a useful function. It uses a simple recursion to work down through the folder tree and a loop to enumerate the files and subfolders.

//
//	Remove all folders.
//
void RemoveAllDirectories(CString strDir)
{
	CFileFind ff;
	CString path = strDir;
	
	path = AddPathAndFile(path,_T("*.*"));
	BOOL res = ff.FindFile(path);

	while(res) {
		res = ff.FindNextFile();
		if (!ff.IsDots()) {
			if(!ff.IsDirectory())
				DeleteFile(ff.GetFilePath());
			else {
				path = ff.GetFilePath();
				RemoveAllDirectories(path);
			}
		}
	}
	RemoveDirectory(strDir);
}

Leave a Reply

(required)

(required)