Windows删除某文件夹下面所有重复的文件

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值