C++实现:把一个文件夹里的冗余文件(.txt)删除
代码很简单,调用了MFC里的几个函数。这里的冗余判断,是要遍历文件内容,进行两两比较。
需要注意的地方有两点:
1.源文件里头文件<afx.h>必须放在最前面。这里是为了避免nafxcwd.lib error LNK2005,由于CRT 库对 new、delete 和 DllMain 函数使用弱外部链接,MFC 库也包含 new、delete 和 DllMain 函数,这些函数要求先链接 MFC 库,然后再链接 CRT 库。
2.MFC库采取静态编译。这里是为了避免nafxcwd.lib error LNK2001。
代码实现:
#include <afx.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<CFileStatus> cf;
bool cmp(const CFileStatus &r1,const CFileStatus &r2){
return r1.m_size<r2.m_size;
} int main(){
CFileFind finder;
CFile cfile;
CFileStatus rStatus;
BOOL bWorking=finder.FindFile(_T("*.txt")); cout<<"正在搜索当前目录下冗余文件..."<<endl;
while (bWorking){
bWorking = finder.FindNextFile();
CString fp = (LPCTSTR)finder.GetFilePath();
CFile::GetStatus(fp,rStatus);
cf.insert(cf.end(), rStatus);
}
sort(cf.begin(),cf.end(),cmp);
for(int i=;i < cf.size(); i++){
int size = cf[i].m_size;
int num = ;
bool tag = false;
CFile file1(cf[i].m_szFullName,CFile::modeRead);
char *FileContent1 = new char[cf[i].m_size];
file1.Read(FileContent1,cf[i].m_size);
for(int j=i+;j < cf.size(); j++){
if(cf[i].m_size == cf[j].m_size){
CFile file2(cf[j].m_szFullName,CFile::modeRead);
char *FileContent2 = new char[cf[j].m_size];
file2.Read(FileContent2,cf[j].m_size);
for(num =; num <size; num++){
if(FileContent1[num] != FileContent2[num]){
break;
}
}
file2.Close();
if(num == size){
cout<<"找到一组冗余文件,正在删除其中冗余文件! "<<endl;
file2.Remove(cf[j].m_szFullName);
cf.erase(cf.begin()+j);
j--;
delete FileContent2;
}
}else break;
file1.Close();
}
}
cout<<"已完成操作!"<<endl;
return ;
}
下面是加了MD5的版本(采取多文件组织):
md5.h:
#ifndef MD5_H
#define MD5_H typedef struct
{
unsigned int count[];
unsigned int state[];
unsigned char buffer[];
}MD5_CTX; #define F(x,y,z) ((x & y) | (~x & z))
#define G(x,y,z) ((x & z) | (y & ~z))
#define H(x,y,z) (x^y^z)
#define I(x,y,z) (y ^ (x | ~z))
#define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
#define FF(a,b,c,d,x,s,ac) \
{ \
a += F(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define GG(a,b,c,d,x,s,ac) \
{ \
a += G(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define HH(a,b,c,d,x,s,ac) \
{ \
a += H(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define II(a,b,c,d,x,s,ac) \
{ \
a += I(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
void MD5Init(MD5_CTX *context);
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen);
void MD5Final(MD5_CTX *context,unsigned char digest[]);
void MD5Transform(unsigned int state[],unsigned char block[]);
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len);
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len); #endif
md5.cpp:
#include <memory.h>
#include "md5.h" unsigned char PADDING[]={0x80,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,}; void MD5Init(MD5_CTX *context)
{
context->count[] = ;
context->count[] = ;
context->state[] = 0x67452301;
context->state[] = 0xEFCDAB89;
context->state[] = 0x98BADCFE;
context->state[] = 0x10325476;
}
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen)
{
unsigned int i = ,index = ,partlen = ;
index = (context->count[] >> ) & 0x3F;
partlen = - index;
context->count[] += inputlen << ;
if(context->count[] < (inputlen << ))
context->count[]++;
context->count[] += inputlen >> ; if(inputlen >= partlen)
{
memcpy(&context->buffer[index],input,partlen);
MD5Transform(context->state,context->buffer);
for(i = partlen;i+ <= inputlen;i+=)
MD5Transform(context->state,&input[i]);
index = ;
}
else
{
i = ;
}
memcpy(&context->buffer[index],&input[i],inputlen-i);
}
void MD5Final(MD5_CTX *context,unsigned char digest[])
{
unsigned int index = ,padlen = ;
unsigned char bits[];
index = (context->count[] >> ) & 0x3F;
padlen = (index < )?(-index):(-index);
MD5Encode(bits,context->count,);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,);
MD5Encode(digest,context->state,);
}
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)
{
unsigned int i = ,j = ;
while(j < len)
{
output[j] = input[i] & 0xFF;
output[j+] = (input[i] >> ) & 0xFF;
output[j+] = (input[i] >> ) & 0xFF;
output[j+] = (input[i] >> ) & 0xFF;
i++;
j+=;
}
}
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len)
{
unsigned int i = ,j = ;
while(j < len)
{
output[i] = (input[j]) |
(input[j+] << ) |
(input[j+] << ) |
(input[j+] << );
i++;
j+=;
}
}
void MD5Transform(unsigned int state[],unsigned char block[])
{
unsigned int a = state[];
unsigned int b = state[];
unsigned int c = state[];
unsigned int d = state[];
unsigned int x[];
MD5Decode(x,block,);
FF(a, b, c, d, x[ ], , 0xd76aa478); /* 1 */
FF(d, a, b, c, x[ ], , 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[ ], , 0x242070db); /* 3 */
FF(b, c, d, a, x[ ], , 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[ ], , 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[ ], , 0x4787c62a); /* 6 */
FF(c, d, a, b, x[ ], , 0xa8304613); /* 7 */
FF(b, c, d, a, x[ ], , 0xfd469501); /* 8 */
FF(a, b, c, d, x[ ], , 0x698098d8); /* 9 */
FF(d, a, b, c, x[ ], , 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[], , 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[], , 0x895cd7be); /* 12 */
FF(a, b, c, d, x[], , 0x6b901122); /* 13 */
FF(d, a, b, c, x[], , 0xfd987193); /* 14 */
FF(c, d, a, b, x[], , 0xa679438e); /* 15 */
FF(b, c, d, a, x[], , 0x49b40821); /* 16 */ /* Round 2 */
GG(a, b, c, d, x[ ], , 0xf61e2562); /* 17 */
GG(d, a, b, c, x[ ], , 0xc040b340); /* 18 */
GG(c, d, a, b, x[], , 0x265e5a51); /* 19 */
GG(b, c, d, a, x[ ], , 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[ ], , 0xd62f105d); /* 21 */
GG(d, a, b, c, x[], , 0x2441453); /* 22 */
GG(c, d, a, b, x[], , 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[ ], , 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[ ], , 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[], , 0xc33707d6); /* 26 */
GG(c, d, a, b, x[ ], , 0xf4d50d87); /* 27 */
GG(b, c, d, a, x[ ], , 0x455a14ed); /* 28 */
GG(a, b, c, d, x[], , 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[ ], , 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[ ], , 0x676f02d9); /* 31 */
GG(b, c, d, a, x[], , 0x8d2a4c8a); /* 32 */ /* Round 3 */
HH(a, b, c, d, x[ ], , 0xfffa3942); /* 33 */
HH(d, a, b, c, x[ ], , 0x8771f681); /* 34 */
HH(c, d, a, b, x[], , 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[], , 0xfde5380c); /* 36 */
HH(a, b, c, d, x[ ], , 0xa4beea44); /* 37 */
HH(d, a, b, c, x[ ], , 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[ ], , 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[], , 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[], , 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[ ], , 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[ ], , 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[ ], , 0x4881d05); /* 44 */
HH(a, b, c, d, x[ ], , 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[], , 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[], , 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[ ], , 0xc4ac5665); /* 48 */ /* Round 4 */
II(a, b, c, d, x[ ], , 0xf4292244); /* 49 */
II(d, a, b, c, x[ ], , 0x432aff97); /* 50 */
II(c, d, a, b, x[], , 0xab9423a7); /* 51 */
II(b, c, d, a, x[ ], , 0xfc93a039); /* 52 */
II(a, b, c, d, x[], , 0x655b59c3); /* 53 */
II(d, a, b, c, x[ ], , 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[], , 0xffeff47d); /* 55 */
II(b, c, d, a, x[ ], , 0x85845dd1); /* 56 */
II(a, b, c, d, x[ ], , 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[], , 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[ ], , 0xa3014314); /* 59 */
II(b, c, d, a, x[], , 0x4e0811a1); /* 60 */
II(a, b, c, d, x[ ], , 0xf7537e82); /* 61 */
II(d, a, b, c, x[], , 0xbd3af235); /* 62 */
II(c, d, a, b, x[ ], , 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[ ], , 0xeb86d391); /* 64 */
state[] += a;
state[] += b;
state[] += c;
state[] += d;
}
main.cpp:
#include <afx.h>
#include <iostream>
#include <vector>
#include <cstring>
#include "md5.h"
using namespace std; struct node{
unsigned char *s;
CString fp;
}tmp; vector<node> cf; int main(){
MD5_CTX md5; CFileFind finder;
CFile cfile;
CFileStatus rStatus;
BOOL bWorking=finder.FindFile(_T("*.txt")); cout<<"正在搜索当前目录下冗余文件..."<<endl;
while (bWorking){
bWorking = finder.FindNextFile();
tmp.fp = (LPCTSTR)finder.GetFilePath();
CFile::GetStatus(tmp.fp,rStatus); CFile file(rStatus.m_szFullName,CFile::modeRead);
char *FileConten = new char[rStatus.m_size];
file.Read(FileConten,rStatus.m_size); MD5Init(&md5);
MD5Update(&md5,(unsigned char*)FileConten,rStatus.m_size);
tmp.s=new unsigned char[];
MD5Final(&md5,tmp.s);
cf.push_back(tmp); file.Close();
}
for(int i=;i < cf.size(); i++){
for(int j=i+;j < cf.size(); j++){
if(strcmp((const char*)cf[i].s,(const char*)cf[j].s)){
cout<<"找到一组冗余文件,正在删除其中冗余文件! "<<endl;
CFile file(cf[j].fp,CFile::modeRead);
file.Close();
file.Remove(cf[j].fp);
delete cf[j].s;
cf.erase(cf.begin()+j);
j--;
}
}
}
cout<<"已完成操作!"<<endl;
return ;
}
C++实现:把一个文件夹里的冗余文件(.txt)删除的更多相关文章
- 用java实现删除文件夹里的所有文件
package com.org.improve.contact; import java.io.File; public class DeletePaper { /** * @param args * ...
- Matlab 读取文件夹里所有的文件
(image = dir('D:\gesture\*.*'); % dir是指定文件夹得位置,他与dos下的dir用法相同. 用法有三种: 1. dir 是指工作在当前文件夹里 2. dir name ...
- 将Temporary文件夹里的Logo文件转移到Logo文件夹
/// <summary> /// 将Temporary文件夹里的Logo文件转移到Logo文件夹 /// </summary> /// <param name=&quo ...
- 在d盘中创建一个文件夹 在文件夹里创建三个txt文本
import java.io.File; import java.io.IOException; public class FileDemo { public static void main(Str ...
- 用java删除文件夹里的所有文件
import java.io.File; public class Test { public static void main(String args[]){ Test t = new Test() ...
- php中如何上传整个文件夹里的所有文件?
1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...
- Android中res/layout文件夹里新建布局文件,R中不生成ID的奇葩错误
新浪微博:http://weibo.com/u/1928100503 网上看了下,发现大都是xml文件名大写而导致的id不能生成的问题,但在下的问题却不是大小写的问题,在下发现,当你的layout目录 ...
- jq和js插件的各个文件夹里放置的内容
1. demo文件夹,存放各种实例. 2. dist文件夹,全称是distribution.在某些框架中,因为开发和发布的内容或者代码形式是不一样的(比如利用Grunt压缩等等),这时候就需要一个存放 ...
- diff两个文件夹里的东西
diff --help -x, --exclude=PAT exclude files that match PAT 排除某个类型的文件 -u, -U NUM, --uni ...
随机推荐
- Java的变量相关
变量是在一个范围内的可变的值. 要点: 数据类型(确定变量的值的类型) 一个字节里面8个位,每个位里存储0101这样的二进制的补码表示用来数据,一个字节的数据类型的第一个位是符号位,表示正负. 数据类 ...
- Android 4.3发布 新增4大改变25日推送升级[附Android 4.3 工厂镜像]
北京时间7月25日,谷歌举行发布会,正式发布了全新的Nexus 7平板电脑以及Android 4.3系统. 其中Android 4.3系统隶属于4.X果冻豆(Jelly Bean)系列,是目前最新的操 ...
- Winform开发常用控件之DataGridView的简单数据绑定——自动绑定
DataGridView控件可谓是Winform开发的重点控件,对于数据的呈现和操作非常方便,DataGridView可谓是既简单又复杂.简单在于其已经集成了很多方法,复杂在于可以使用其实现复杂的数据 ...
- rancher下的kubernetes之二:安装rancher和kubernetes
在上一章<rancher下的kubernetes之一:构建标准化vmware镜像>,我们做了个通用的虚拟机镜像,可以root登录,apt已经更新,docker也装好了,现在我们就来安装ra ...
- spring注解-@Autowired。@Resource。@Service
Spring的@Autowired注解.@Resource注解和@Service注解 什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: ...
- BZOJ4012: [HNOI2015]开店【动态点分治】
Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...
- 常用PHP框架收集
1.ThinkCMFX http://git.oschina.net/thinkcmf/ThinkCMFX 2.ThinkPHP http://www.thinkphp.cn/down.html 3. ...
- Ext.js基础
第一章:Ext.js基础 好书推荐 Javascript设计模式 征服ajax web 2.0开发技术详解 简介 基础要求 了解HTML.CSS.熟练JS.JS的OOP.AJAX JSP/PHP/AS ...
- 智能家居入门DIY——【六、使用OneNet后台处理数据】
OneNet使用起来要比lewei50复杂一些,它没有前台需要自己开发.命令下发也和之前介绍的lewei50有一些区别,这里着重介绍一下使用MQTT协议来进行通讯. 一.准备 1.Esp8266开发板 ...
- HyperLogLog(不精确的去重计数方案)
pfadd 用法和sadd一样 pfcount 用法和scard一样 127.0.0.1:6379> get lan (nil) 127.0.0.1:6379> pfadd lan js ...