zoukankan      html  css  js  c++  java
  • vc 递归删除非空文件夹

    我觉得这是一个非常不错的递归例子

    头文件

    #pragma once
    #include <atlstr.h>
    #include <io.h>
    #include <string>
    #include <iostream>
    #include <windows.h>

    using namespace std;

    BOOL Deleteall(CString path)
    {
     long handle = -1;   //用于查找的句柄
     CString strFilePath = "";
     CString strLog = "";
     strFilePath = strPath + "*.*";

     struct _finddata_t fileinfo;   //文件信息的结构体

     handle=_findfirst(strFilePath,&fileinfo);
     if (handle != -1)
     {
      do
      {
       int isSubDir = fileinfo.attrib & _A_SUBDIR;
       if(isSubDir)                                   //如果是文件夹
       {
        //CString FileName = fileinfo.name;
        string strFileName(fileinfo.name );
        if(strFileName.compare(".") != 0 && strFileName.compare("..") != 0)
        {
         CString NewPath = strPath + strFileName.c_str() + "\" ;
         DeleteAllFile(NewPath);         //递归
        }
       }
       else
       {
        CString str1(strPath);
        string str2(fileinfo.name );
        CString strfilepath = str1 + str2.c_str();
        if(!DeleteFile(strfilepath))
        {
         strLog.Format("delete %s is failed,errorCode :%d ", str2, GetLastError());
         cout<<strLog;
        }
       }
      }while(_findnext(handle, &fileinfo) != -1); // 遍历此目录下所有文件找配置文件??
      _findclose(handle);

      if(!RemoveDirectory(strPath))
      {
       strLog.Format("delete %s is failed errorCode :%d ", strPath, GetLastError());
       cout<<strLog;
       return FALSE;
      }
      else
      {
       strLog.Format("delete %s is succeed ", strPath);
       cout<<strLog;
      }
     }
     return 0;

    }

    main文件

    void main()
    {
    CString path = "D:\work\" ;

    if(DeleteFiles(path))
       cout<<"delete succeed"<<endl;
    else
       cout<<"delete fail"<<endl;
    }

  • 相关阅读:
    数据类型转换(日期格式转换)
    TextArea控件实时计算总字数,总行数,和每行显示的最大字数
    Java--->判断IP和端口是否可连接
    JavaFX校验IP和端口的合法性
    JavaFX与NetBeans开发工具的一些总结
    Web Service深度剖析
    Spring中AOP和IOC深入理解
    Spring aspect 两种方式实现五种增强
    Struts2错题总结
    Hibernate检索方式和Criteria查询的讲解
  • 原文地址:https://www.cnblogs.com/sharecenter/p/5621004.html
Copyright © 2011-2022 走看看