C++ 删除指定路径文件夹

来源:互联网 发布:android js框架 编辑:程序博客网 时间:2024/06/07 06:01

/************************************************************************/
/* 删除指定路径下的文件夹
/* DirName: 文件路径
/* bNeedDel: 是否删除文件夹                                         
/************************************************************************/
void DeleteDirectory(char *DirName, bool bNeedDel)
{
 CFileFind tempFind;   
 char tempFileFind[MAX_PATH];   
 sprintf(tempFileFind,"%s\\*.*",DirName);   
 BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);    //判断文件夹下是否存在文件
 while(IsFinded)    //存在
 {       
  IsFinded=(BOOL)tempFind.FindNextFile(); //判断是否存在下一个文件      
  if(!tempFind.IsDots())       
  {           
   char foundFileName[MAX_PATH] = {0};           
   strcpy(foundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));           
   if(tempFind.IsDirectory())           
   {               
    char tempDir[MAX_PATH] = {0};               
    sprintf(tempDir,"%s\\%s",DirName,foundFileName);              
    DeleteDirectory(tempDir, bNeedDel);           
   }           
   else           
   {               
    char tempFileName[MAX_PATH] = {0};               
    sprintf(tempFileName,"%s\\%s",DirName,foundFileName);               
    DeleteFile(tempFileName);           
   }       
  }   
 } 
 
 tempFind.Close(); 
 
 if (bNeedDel)
 {
  RemoveDirectory(DirName);
 }
}
0 0
原创粉丝点击