c++ 删除文件夹

BOOL DeleteDirectory(CString DirName,BOOL bRemoveRootDir)
 
 
{
    
    CFileFind tempFind;
    CString csZRootDir;
 
 
    csZRootDir.Format(L"%s\\*.*",DirName.GetBuffer(0));
    
    BOOL IsFinded=(BOOL)tempFind.FindFile(csZRootDir.GetBuffer(0));
    
    while(IsFinded)
        
    {
        IsFinded=(BOOL)tempFind.FindNextFile();
        
        if(!tempFind.IsDots())
            
        {
            CString csFileName;
 
 
            csFileName.Format(L"%s",tempFind.GetFileName().GetBuffer(0));    
            if(tempFind.IsDirectory())
                
            {
                CString tmp;
                tmp.Format(L"%s\\%s",DirName.GetBuffer(0),csFileName.GetBuffer(0));
                                
                DeleteDirectory(tmp,TRUE);
                
            }
            
            else
                
            {
                CString tmp;
                tmp.Format(L"%s\\%s",DirName,csFileName.GetBuffer(0));
                
                DeleteFile(tmp.GetBuffer(0));
                
            }
            
        }
        
    }
    
    tempFind.Close();
    if (bRemoveRootDir)
    {
        if(!RemoveDirectory(DirName))
            
        {
            //LogRecord(TRUE,_T("remove directory failed! %s \r\n"),DirName);
            
            return FALSE;    
        }
    }
     
    
    return TRUE;
    
}

猜你喜欢

转载自blog.csdn.net/Sayesan/article/details/89229323