删除目录及目录下所有文件与子目录

VC++只提供了删除一个空目录的函数,而用往往希望删除其下有很多子目录与
文件的目录。为了实现这一功能,我编写了DeleteDirectory 函数,它可以实现
这一功能。

函数原型:BOOL DeleteDirectory(char *DirName);
返回值:成功删除时返回TRUE,否则返回FALSE
参数DirName为要删除的目录名,必须为绝对路径名,如“c:\\temp"。
函数定义如下
BOOL DeleteDirectory(char *DirName)
{
    CFileFind tempFind;
    
char tempFileFind[200];
    sprintf(tempFileFind,
"%s\\*.*",DirName);
    BOOL IsFinded
=(BOOL)tempFind.FindFile(tempFileFind);
    
while(IsFinded)
   
{
       IsFinded
=(BOOL)tempFind.FindNextFile();
       
if(!tempFind.IsDots())
       
{
             
char foundFileName[200];
             strcpy(foundFileName,tempFind.GetFileName().GetBuffer(
200));
             
if(tempFind.IsDirectory())
             
{
                   
char tempDir[200];
                   sprintf(tempDir,
"%s\\%s",DirName,foundFileName);
                   DeleteDirectory(tempDir);
              }

              
else
             
{
                   
char tempFileName[200];
                   sprintf(tempFileName,
"%s\\%s",DirName,foundFileName);
                   DeleteFile(tempFileName);
              }

         }

   }

   tempFind.Close();
   
if(!RemoveDirectory(DirName))
  
{
       MessageBox(
0,"删除目录失败!","警告信息",MK_OK);
       
return FALSE;
   }

   
return TRUE;
}
 
posted on 2005-12-25 02:22  王浩的博客  阅读(477)  评论(0编辑  收藏  举报