/*
删除指定目录以及目录下的所有文件
*/
#include <iostream>
#include <io.h>
#include <direct.h>
using namespace std;
void listFiles(string dir);
int main() {
//目录路径
string dir = "需要删除的目录";
listFiles(dir);
system("pause");
return 1;
}
void listFiles(string dir) {
//在目录后面加上"\\*.*"进行第一次搜索
string newDir = dir + "\\*.*";
//用于查找的句柄
intptr_t handle;
struct _finddata_t fileinfo;
//第一次查找
handle = _findfirst(newDir.c_str(), &fileinfo);
if (handle == -1) {
cout << "无文件" << endl;
system("pause");
return;
}
do
{
if (fileinfo.attrib & _A_SUBDIR) {//如果为文件夹,加上文件夹路径,再次遍历
if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)
continue;
// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
newDir = dir + "\\" + fileinfo.name;
listFiles(newDir.c_str());//先遍历删除文件夹下的文件,再删除空的文件夹
cout << newDir.c_str() << endl;
if (_rmdir(newDir.c_str()) == 0) {//删除空文件夹
cout << "delete empty dir success" << endl;
}
else {
cout << "delete empty dir error" << endl;
}
}
else{
string file_path = dir + "\\" + fileinfo.name;
cout << file_path.c_str() << endl;
if (remove(file_path.c_str()) == 0) {//删除文件
cout << "delete file success" << endl;
}else{
cout << "delete file error" << endl;
}
}
} while (!_findnext(handle, &fileinfo));
_findclose(handle);
return;
}
C++基础,删除指定文件夹以及文件夹下的所有文件
最新推荐文章于 2024-05-11 18:41:21 发布