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

头文件

#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\n", str2, GetLastError());

     cout<<strLog;

    }

   }

  }while(_findnext(handle, &fileinfo) != -1); // 遍历此目录下所有文件找配置文件??

  _findclose(handle);

if(!RemoveDirectory(strPath))

  {

   strLog.Format("delete %s is failed errorCode :%d\n", strPath, GetLastError());

   cout<<strLog;

   return FALSE;

  }

  else

  {

   strLog.Format("delete %s is succeed\n", strPath);

   cout<<strLog;

  }

 }

 return 0;

}

main文件

void main()

{

CString path = "D:\\work\\" ;

if(DeleteFiles(path))

   cout<<"delete succeed"<<endl;

else

   cout<<"delete fail"<<endl;

}

vc 递归删除非空文件夹的更多相关文章

  1. NodeJs递归删除非空文件夹

    此篇博文由于第一次使用fs.unlink()删除文件夹时报“Error: EPERM: operation not permitted, unlink”错误而写,这是因为fs.unlink()只能删除 ...

  2. 如何使用python移除/删除非空文件夹?

    移除/删除非空文件夹/目录的最有效方法是什么? 1.标准库参考:shutil.rmtree. 根据设计,rmtree在包含只读文件的文件夹树上失败.如果要删除文件夹,不管它是否包含只读文件,请使用 i ...

  3. 【转】 python 删除非空文件夹

    转自:https://blog.csdn.net/xiaodongxiexie/article/details/77155864 一般删除文件时使用os库,然后利用os.remove(path)即可完 ...

  4. windows C++删除非空文件夹

    //add by zhuxy 递归删除文件夹 BOOL myDeleteDirectory(CString directory_path) //删除一个文件夹下的所有内容 { BOOL ret=TRU ...

  5. python 删除非空文件夹

    import os import shutil os.remove(path) #删除文件 os.removedirs(path) #删除空文件夹 shutil.rmtree(path) #递归删除文 ...

  6. C 实现删除非空文件夹

    /* 文件名:   rd.c ---------------------------------------------------- c中提供的对文件夹操作的函数,只能对空文件夹进行 删除,这使很多 ...

  7. mac 下删除非空文件夹

    Linux中rmdir命令是用来删除空的目录.使用方式: rmdir [-p] dirName 参数: -p 是当子目录被删除后使它也成为空目录的话,则顺便一并删除. 举例说明:rmdir folde ...

  8. QT删除非空文件夹

    int choose; choose = QMessageBox::warning(NULL,"warning","确定删除该文件?",QMessageBox: ...

  9. shell命令rm删除非空文件夹

    rm -rf dirName CentOS的自带的资源管理器叫nautilus,在命令行里输入nautilus可以启动它.

随机推荐

  1. LintCode "Expression Tree Build"

    Lesson learnt: for any calculator problems, keep 2 stacks: 1 for operators and 1 for operands. class ...

  2. 去掉IntelliJ IDEA的拼写检查

    Settings→Editor→Inspections→Spelling 去掉Spelling下的Typo复选框即可 来自为知笔记(Wiz)

  3. Node.js 相关资料网站汇总

    地址:https://cnodejs.org/ nodejs中文网:http://nodejs.cn/ nodejs中文网:http://www.nodejs.net/ 相关API地址:http:// ...

  4. js延迟加载,提升网页加载速度

    JS延迟加载,简单例子,不多说: 代码如下: 程序代码 <script language="JavaScript" src="" id="my& ...

  5. (C/C++) memset

    C语言: memset   extern void *memset(void *buffer,int c,int count);   #include <string.h>   功能:把b ...

  6. ADF_Starting系列5_使用ADF开发富Web应用程序之维护User Interface(Part2)

    2014-05-05 Created By BaoXinjian

  7. POJ 1611 The Suspects(并查集,简单)

    为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...

  8. angular.js form

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. linuc c 代码示例

    fork的应用: #include "stdio.h" #include "string.h" #include <sys/types.h> #in ...

  10. HTML5表单与PHP交互

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...