Click to See Complete Forum and Search --> : Deleting Directories


PJTewkesbury
April 21st, 1999, 08:02 AM
I am trying to delete a directory using the commands DeleteFile & RemoveDirectory, but I get 'Access is denied.' or 'The process cannot access the file because it is being used by another process.'

I am using WinNT4(sp4) VC6 (sp2). The program I am writing is MFC based.

Thakn you for your time.


Peter Tewkesbury
Software Engineer
NedGraphics Print LTD

sally
April 29th, 1999, 08:12 PM
you can not delete c:\f1 in one go if c:\f1 has sub folders
you have to delete each sub folder first and then c:\f1, recursively

Sally

Sally
April 29th, 1999, 08:12 PM
you can not delete c:\f1 in one go if c:\f1 has sub folders
you have to delete each sub folder first and then c:\f1, recursively

Sally

John_Reese
May 27th, 1999, 08:30 AM
I am having a similar problem to yours with an mfc based program. Have you found a fix? If so, could you please help me out? Thanks...

John_Reese
May 27th, 1999, 08:57 AM
I forgot to tell you earlier...There is a nice delete directory function here on codegure that someone wrote...You can search for it under NukeDirectory. The code works, I am using it. It will delete all files and subfolders within a given directory, then the directory. It takes &strDir as a parameter.

yash
May 27th, 1999, 10:08 AM
Hi ,
Here is the code which deletes the dir recersively .


BOOL DeleteDirectory( CString DirectoryPath )
{
CFileFind finder;
DirectoryPath += "\\*.*";

if ( (DirectoryPath == "" || ( DirectoryPath == "c:\" ))
return TRUE ; // make sure don't delete ROOT dir !


BOOL bWorking = finder.FindFile(DirectoryPath);

while (bWorking)
{
bWorking = finder.FindNextFile();
if (!finder.IsDots()) //skip the dots which denote the current directory
{
DeleteFile((LPCTSTR)finder.GetFilePath());
if (finder.IsDirectory())
{
DeleteDirectory(finder.GetFilePath());
}
}
}
finder.Close();

if (!RemoveDirectory(LPCTSTR( DirectoryPath.Left(DirectoryPath.GetLength()-strlen("\\*.*")) + '\0')))
{
return FALSE;
}
return TRUE;
}





hope this helps !

Good Luck.
Yash.

Ravi Bhavnani
May 27th, 1999, 12:13 PM
It's important to ensure that the directory you're attempting to delete is not your default directory. So, just before you blow it away, _chdir to another directory.

/ravi