C++递归删除非空目录所有文件
2018-03-29 22:14
4435 查看
今天在写C++代码时,遇到一个小问题,也是搞了一下午才解决(惭愧),就是在某个目录下,保存的相同名称的图片没有自动覆盖,意思就是多次运行相同的程序,如果某个文件(名)存在的话,那么这个文件则不会被覆盖,文件还是最早保留的那一文件。
在网上查了下,Window API函数(需要包含头文件include<windows.h>)RemoveDirectory()函数等很多方法只能删除非空目录,可以参考https://blog.csdn.net/weixin_36340947/article/details/77937651
下面是找到的参考代码,亲测有效,一定要注意目录的路径问题,以及删除目录所有文件一定要慎重,可以自己多测试几次,熟悉了之后再使用。#include<iostream>
#include<string>
#include<io.h>
using namespace std;
//递归删除指定文件夹下所有的文件(目前好像删不了文件夹)
int removeDir(string dirPath)
{
struct _finddata_t fb; //find the storage structure of the same properties file.
string path;
long handle;
int resultone;
int noFile; // the tag for the system's hidden files
noFile = 0;
handle = 0;
path = dirPath + "/*";
handle = _findfirst(path.c_str(), &fb);
//find the first matching file
if (handle != -1)
{
//find next matching file
while (0 == _findnext(handle, &fb))
{
// "." and ".." are not processed
noFile = strcmp(fb.name, "..");
if (0 != noFile)
{
path.clear();
path = dirPath + "/" + fb.name;
//fb.attrib == 16 means folder
if (fb.attrib == 16)
{
removeDir(path);
}
else
{
//not folder, delete it. if empty folder, using _rmdir istead.
remove(path.c_str());
}
}
}
// close the folder and delete it only if it is closed. For standard c, using closedir instead(findclose -> closedir).
// when Handle is created, it should be closed at last.
_findclose(handle);
}
return 0;
}
void main() {
removeDir(".\\test");
} 直接调用removeDir()函数就行了,输入参数为你要删除的目录的路径(可以为字符串形式),当然可以是相对路径,比如我要删除的目录在工程目录下,路径就可以写成“.\\needRemoveDir”,当然你也可以写成绝对路径,就是下次工程项目换地方的时候需要注意下就可以了。
在网上查了下,Window API函数(需要包含头文件include<windows.h>)RemoveDirectory()函数等很多方法只能删除非空目录,可以参考https://blog.csdn.net/weixin_36340947/article/details/77937651
下面是找到的参考代码,亲测有效,一定要注意目录的路径问题,以及删除目录所有文件一定要慎重,可以自己多测试几次,熟悉了之后再使用。#include<iostream>
#include<string>
#include<io.h>
using namespace std;
//递归删除指定文件夹下所有的文件(目前好像删不了文件夹)
int removeDir(string dirPath)
{
struct _finddata_t fb; //find the storage structure of the same properties file.
string path;
long handle;
int resultone;
int noFile; // the tag for the system's hidden files
noFile = 0;
handle = 0;
path = dirPath + "/*";
handle = _findfirst(path.c_str(), &fb);
//find the first matching file
if (handle != -1)
{
//find next matching file
while (0 == _findnext(handle, &fb))
{
// "." and ".." are not processed
noFile = strcmp(fb.name, "..");
if (0 != noFile)
{
path.clear();
path = dirPath + "/" + fb.name;
//fb.attrib == 16 means folder
if (fb.attrib == 16)
{
removeDir(path);
}
else
{
//not folder, delete it. if empty folder, using _rmdir istead.
remove(path.c_str());
}
}
}
// close the folder and delete it only if it is closed. For standard c, using closedir instead(findclose -> closedir).
// when Handle is created, it should be closed at last.
_findclose(handle);
}
return 0;
}
void main() {
removeDir(".\\test");
} 直接调用removeDir()函数就行了,输入参数为你要删除的目录的路径(可以为字符串形式),当然可以是相对路径,比如我要删除的目录在工程目录下,路径就可以写成“.\\needRemoveDir”,当然你也可以写成绝对路径,就是下次工程项目换地方的时候需要注意下就可以了。
相关文章推荐
- 递归删除一个非空目录及其下所有文件和目录
- asp.net 删除文件夹,指定文件夹,删除文件夹和所有文件,删除权限设置,递归删除文件夹目录及文件
- 文件的两种递归操作 删除目录下所有文件和输出目录下所有文件路径和名
- java实现递归删除目录及目录下所有文件,递归和非递归方式都有
- 递归解压当前目录以及子目录下的所有压缩文件,并删除源文件
- asp.net 删除文件夹,指定文件夹,删除文件夹和所有文件,删除权限设置,递归删除文件夹目录及文件
- 递归删除某个目录及目录下的所有子目录和文件
- 递归删除指定目录下所有文件及子目录
- 递归删除目录下所有文件
- java单个文件复制,递归删除、复制目录下所有内容,递归创建文件夹,.7z文件解压等
- MFC中递归删除目录下的所有文件以及父目录
- 递归解压当前目录以及子目录下的所有压缩文件,并删除源文件
- 递归的一些方法使用:深度遍历路径中的所有文件和目录,删除一个有文件的文件夹
- Java递归删除目录下所有的txt文件
- Linux命令:递归删除目录下所有exe文件
- 递归遍历指定目录下所有指定的文件并删除
- FAQ2004:删除非空目录下所有文件
- C++ 删除、重命名文件 、获取当前目录所有文件代码
- linux系统调用之文件:递归删除非空目录
- 递归删除指定目录下的指定文件夹及其所有文件