Click to See Complete Forum and Search --> : How to delete all files in a directory?


dearphael
April 16th, 1999, 01:05 AM
Hi,

How do I delete all files in a directory? Right now I am using
system("del c:\directory\*.*)
but it shows a DOS box which is not desirable

Dave Lorde
April 16th, 1999, 07:39 AM
Use FindFirstFile/FindNextFile with DeleteFile.

Dave

yash
April 16th, 1999, 08:58 AM
Hi,

below is the code to delete the directory!

BOOL DeleteDirectory( CString DirectoryPath )
{

if ( DirectoryPath.IsEmpty()) //make sure not to delete the Root !
return TRUE ;

CFileFind finder;
DirectoryPath += "\\*.*";

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 You.

Good Luck.
Yash.

dearphael
April 17th, 1999, 12:37 AM
Hi,

Thank you all for the prompt replies.