博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用CFileFind遍历递归删除文件和文件夹
阅读量:4034 次
发布时间:2019-05-24

本文共 2148 字,大约阅读时间需要 7 分钟。

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

你可能感兴趣的文章
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
Phone双模修改涉及文件列表
查看>>
android UI小知识点
查看>>
Android之TelephonyManager类的方法详解
查看>>
android raw读取超过1M文件的方法
查看>>
ubuntu下SVN服务器安装配置
查看>>
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>