看流星社区

 找回密码
 注册账号
查看: 2241|回复: 0

递归删除一个目录

[复制链接]

该用户从未签到

发表于 2017-6-2 13:28:27 | 显示全部楼层 |阅读模式
CFindFile的使用框架:
void Recurse(LPCTSTR pstr)
{
   CFileFind finder;

   // build a string with wildcards
   CString strWildcard(pstr);
   strWildcard += _T("\\*.*");

   // start working for files
   BOOL bWorking = finder.FindFile(strWildcard);

   while (bWorking)
   {
      bWorking = finder.FindNextFile();

      // skip . and .. files; otherwise, we'd
      // recur infinitely!

      if (finder.IsDots())
         continue;

      // if it's a directory, recursively search it

      if (finder.IsDirectory())
      {
         CString str = finder.GetFilePath();
         TRACE(_T("%s\n"), (LPCTSTR)str);
         Recurse(str);
      }
   }

   finder.Close();
}





递归删除

//循环删除一个目录
void RecursiveDelete(CString strDir)
{
        CFileFind ff;
        CString strPath;
        strPath = strDir;
        if (strPath.Right(1) != '\\')
        {
                strPath += '\\';
        }
        strPath += "*.*";

        BOOL bWorking = ff.FindFile(strPath);
        while (bWorking)
        {
                bWorking = ff.FindNextFile();

                // skip . and .. files; otherwise, we'd
                // recur infinitely!
                if (ff.IsDots())
                        continue;

                // if it's a directory, recursively search it

                if (ff.IsDirectory())
                {
                        //递归目录
                        CString str = ff.GetFilePath();
                        TRACE(_T("%s\n"), (LPCTSTR)str);
                        RecursiveDelete(str);
                        //删除目录
                        ::SetFileAttributesA(str, FILE_ATTRIBUTE_NORMAL);
                        ::RemoveDirectory(str);
                }
                else
                {
                        //删除文件
                        CString str = ff.GetFilePath();
                        TRACE(_T("%s\n"), (LPCTSTR)str);
                        ::SetFileAttributes(str, FILE_ATTRIBUTE_NORMAL);
                        :eleteFile(str);
                }
        }

        ff.Close();

       


}
int main(int argc, char *argv[])
{
        RecursiveDelete("C:\\20_128\\");
        return 0;
}
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-29 18:40

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表