使用CFileFind遍历递归删除文件和文件夹

来源:互联网 发布:知乎 正常感情浓度 编辑:程序博客网 时间:2024/05/05 00:50

1.


    void   DeleteDirectory(CString   strDir)        {          if(strDir.IsEmpty())          return;                   //   首先删除文件及子文件夹          CFileFind   ff;          BOOL   bFound   =   ff.FindFile(strDir+"\\*",   0);          while(bFound)          {          bFound   =   ff.FindNextFile();          if(ff.GetFileName()=="."||ff.GetFileName()=="..")          continue;          //   去掉文件(夹)只读等属性          SetFileAttributes(ff.GetFilePath(),   FILE_ATTRIBUTE_NORMAL);          if(ff.IsDirectory())          {          //   递归删除子文件夹          DeleteDirectory(ff.GetFilePath());          RemoveDirectory(ff.GetFilePath());          }          else          {          //   删除文件          DeleteFile(ff.GetFilePath());          }          }          ff.Close();                   //   然后删除该文件夹          RemoveDirectory(strDir);        }  

2。

    BOOL   DeleteDirectory(LPCTSTR   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))            {            AfxMessageBox("删除目录失败!",MB_OK);            return     FALSE;            }            return     TRUE;                     }    


转载自:http://xbgd.iteye.com/blog/663025


参考

http://blog.csdn.net/abyss521/article/details/8457998

原创粉丝点击