Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the following method to remove a directory including any files that it contains. The files get deleted fine, but it always fails to delete the directories. I have deleted the directories successfully from a command prompt, but using the code below, or a call to system() with the same command used at the command prompt, the directories are not deleted.
As you can see, I ensure I set the current directory to something other than the one I am deleting (which is on my H drive).

C++
bool Workspace::DeleteDirectory(CString directoryPath)
{
    CFileFind finder;
    // build a search string with wildcards
    CString searchString(directoryPath);
    searchString += _T("\\*.*");
    // start searching for files
    BOOL bSearching = finder.FindFile(searchString);
    while (bSearching)
    {
        bSearching = finder.FindNextFile();
        //skip . and .. files, as they represent thecurrent directory and parent directory
		//markers while iterating through files. Otherwise an infinte loop would occur.
        if (finder.IsDots())
		{
            continue;
		}
        // if it's a directory, recursively delete the files
        if (finder.IsDirectory())
        {
            CString directoryPath = finder.GetFilePath();//condense
            DeleteDirectory(directoryPath);
        }
		else
		{
			//delete the file
			//CT2A converts CString to const char*
			if(remove(CT2A(finder.GetFilePath())))
			{
				//0 is success so file removal has failed
				AfxMessageBox(_T("File: ") + finder.GetFilePath() + _T(" could not be removed"));
				finder.Close();
				return false;
			}
		}
    }
	//not searching anymore as all files have been deleted, so delete the directory
	SetCurrentDirectory(_T("C:\\"));
	
	if(!RemoveDirectory(directoryPath))
	{
		AfxMessageBox(_T("Directory: ") + directoryPath + _T(" could not be removed"));
		finder.Close();
		return false;
	}
	else
	{
		finder.Close();
		return true;
	}
}


I'll be very grateful for any help.
Posted
Comments
Timberbird 17-Nov-11 9:55am    
What does GetLastError() say?
Could it be due to missing finder.Close() ?
Jackie Lloyd 17-Nov-11 10:27am    
You are both right (for which i am extremely grateful). I moved finder.Close() to above the call to RemoveDirectory() and the directory was finally deleted.
However, I am really puzzled as to why the file was deleted ok, as i didn't do finder.Close(0 before remove(). Do you have any idea why?
Chuck O'Toole 17-Nov-11 10:13am    
Yeah, I was thinking you want to move the finder.Close() to before you try to delete the directory.
Jackie Lloyd 17-Nov-11 10:27am    
You are both right (for which i am extremely grateful). I moved finder.Close() to above the call to RemoveDirectory() and the directory was finally deleted.
However, I am really puzzled as to why the file was deleted ok, as i didn't do finder.Close(0 before remove(). Do you have any idea why?
Chuck O'Toole 17-Nov-11 10:39am    
My guess is that the finder object has the directory that it is searching "open" so you could not delete the directory as it was "in use". The files themselves however, were not "open". finder only returns the names, it doesn't try to open the files.

1 solution

I know you got it working now but a far simpler approach would be to use SHFileOperation with FO_DELETE.
 
Share this answer
 
Comments
Jackie Lloyd 17-Nov-11 10:53am    
Thankyou for your suggestion. Does SHFileOperation delete an entire directory (possibly containing directories as well as files) in one go? If so, i am wondering what happens one of the files are open - will the whole method call fail?
Nish Nishant 17-Nov-11 10:54am    
Yes it can handle non-empty folders. The call will fail if a file is in use (same as when you delete from Explorer)
Jackie Lloyd 17-Nov-11 11:42am    
You have all been amazingly helpful. I have also got SHFileop to work, having been previously scared off by all the confsuing parameters that needed to be set! However, when a request is made to delete a directory, my code needs to check all sub-directories for any files that have been opened externally to my application and which would therefore cause the directory deletion to fail. I don't want anything to be deleted if this is the case, and i'd like to know which files are open. Is there any way to do this?
Nish Nishant 17-Nov-11 11:45am    
Since that's a new question/topic, I'd suggest that you start a new thread for that. Keeping track of opened files in your application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900