#include <bits/stdc++.h>
#include <io.h>
#include <stdio.h>
#include <windows.h>
using namespace std;
typedef long long LL;
unordered_set<string> postfix;
unordered_set<string> files;
bool flg = false;
bool TraverseDirectory(string path)
{
LL Handle;
struct __finddata64_t FileInfo;
string strFind = path + "\\*";
if ((Handle = _findfirst64(strFind.c_str(), &FileInfo)) == -1L)
{
printf("没有找到匹配的项目\n");
return false;
}
do
{
//判断是否有子目录
if (FileInfo.attrib & _A_SUBDIR)
{
//判断是子文件夹
//下面的判断条件很重要,过滤 . 和 ..
if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
{
string newPath = path + "\\" + FileInfo.name;
// cout << newPath << endl;
TraverseDirectory(newPath);
}
}
else //判断是文件
{
flg = true;
// string newPath = path + "\\" + FileInfo.name;
// vec_path.push_back(newPath);
// cout << path << endl;
// cout << files.count(path) << endl;
// string t = get(path);
// cout << files.count(t) << endl;
// cout << t << endl;
files.insert(path);
}
} while (_findnext64(Handle, &FileInfo) == 0);
_findclose(Handle);
return true;
}
void color(int x)//函数,这个只有正整数可以产生颜色,小数,负数都是原来的字
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);//(改色)
}
int main()
{
postfix.insert("(1).txt");
postfix.insert("(1).doc");
postfix.insert("(1).docx");
postfix.insert("(1).zip");
postfix.insert("(1).ppt");
postfix.insert("(1).pptx");
postfix.insert("(1).pdf");
cout << "输入文件路径:" << endl;
string path = "";
getline(cin, path);
files.insert(path);
if(TraverseDirectory(path) && flg)
{
for(auto &f : files)
{
if(f == ""){
color(4);
cout << "执行出错!!!" << endl;
return -1;
}
// string cmd = "del " + f + "\\*(1).docx";
// char *c = (char*)cmd.c_str();
// system(c);
for(auto& s : postfix)
{
string del = "del " + string(1,'\"') + f + string(1,'\\')+ "\\*" + s + '"';
char *delc=(char*)del.c_str();
// cout << delc <<"------------------------"<< endl;
system(delc);
}
}
color(2);
cout <<"删除成功!!!"<<endl;
}
else
{
color(4);
cout << "无重复或者执行失败:可能原因文件名存在特殊字符(空格)!!!" <<endl;
}
system("pause");
return 0;
}
Windows删除某文件夹下面所有重复的文件
最新推荐文章于 2024-07-19 16:06:05 发布