Reputation: 563
I have a MFC application where I am facing a problem with folder delete. When I am deleting a folder along with all containing files, it shows deleted from application file list but it shows in Windows explorer but remains not-accessible.When I close the application it the folder vanishes from windows explorer. Why is this happening? I have seen RemoveDirectory()
in the following code is returning zero. But if it is not deleted, then why does it goes away after closing the application. The function is as follows:
void SomeClass::DeleteFileFolder(CString filePath)
{
CFileFind finder;
CString strWildcard(filePath);
strWildcard += _T("\\*.*");
CString str = "";
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
{
CString sFile = finder.GetFileName();
if(sFile !="." && sFile !="..")
{
CString sFile = finder.GetFileName();
str = finder.GetFilePath();
BOOL bDel = DeleteFile(str);
}
}
else if (finder.IsDirectory())
{
CString sDir = finder.GetFileName();
str = finder.GetFilePath();
if((str !=".") && (str !="..") && (str != ".svn"))
{
DeleteFileFolder(str);
BOOL bDel = RemoveDirectory(str);
}
}
else
{
CString sFile = finder.GetFileName();
str = finder.GetFilePath();
BOOL bDel = DeleteFile(str);
}
}
BOOL bDel = RemoveDirectory(filePath);
finder.Close();
}
Please guide.
Upvotes: 1
Views: 2964
Reputation: 493
Sounds like an open handle to some file/folder that's not closed until termination or perhaps that it's somehow set as the current working folder for the application.
Did you consider using SHFileOperation instead? You're reinventing the wheel.
SHFILEOPSTRUCT shfo = {
NULL,
FO_DELETE,
path,
NULL,
FOF_SILENT | FOF_NOERRORUI | FOF_NOCONFIRMATION,
FALSE,
NULL,
NULL };
SHFileOperation(&shfo);
The "path" variable needs to be doubly null terminated.
Upvotes: 2