//******************************************************************// // Build by zengwenwu @ 2012.03.27. //功能: // 1.实现磁盘任意文件的查找,删除,并以日志形式输出。 // 可以实现任意后缀文件的查找,不需要加*,直接输入doc、txt、mp3 // 即可实现相关类型文件的查找。 // 2.可以读取配置文件,也可以在cmd命令行中执行,也可以双击启动运行 // 进行查找删除操作。 // contact me:zww0815@gmail.com //******************************************************************// #include <WINDOWS.H> #include <STDIO.H> #include <STRING.h> #include <conio.h> ////////////////////////////////////////// //要删除的目录 #define FIND_FILE_PATH "C:\\Program Files" //要删除的文件类型 #define FIND_FILE_TYPE ".exe" ////////////////////////////////////////// #define FILE_NAME_TYPE "*.*" #define FLAG_VIEW_FILE 0 #define FLAG_DELETE 1 #define FLAG_FILENAME_TIME 1 #define FLAG_FILENAME_ZHID 0 //最大的文件查找字节数 #define MAX_FIND_PATH 1000 //函数返回值: #define RTN_OK (0) #define RTN_ERR (1) //休眠秒数 #define SLEEP_SECOND 100 //函数声明 void PrintLocalTime(void* flag); void fOpenFileFp(FILE** fp); void fCloseFileFp(FILE** fp); void VisitAllFiles(char * lpPath); void GetCurSysTime(char* str); //全局的文件计数与文件清理计数 long g_lCheckFileNumber; long g_lCheckOkFileNumber; long g_lClearFileNumber; int g_flagDeleteFile; ///////////////////////////////////////////// ///全局的指针变量 /* 定义全局的FILE类型指针,供写日志 */ FILE* g_fpLog; FILE* g_fpCheckOK; ///////////////////////////////////////////// //定义全局的查找文件路径及类型 char g_strFindFilePath[MAX_FIND_PATH]; char g_strFindFileType[10]; //全局的时间字符串 char g_pStrTime[50]; //全局的文件路径名字符串 char g_strTemFileName[80]; //函数访问次数 int g_iCount = 0; //同时打印输出到文件及屏幕上 void WriteFormatted (FILE * stream, char * format, ...) { //获取时间信息 GetCurSysTime(g_pStrTime); va_list args; va_start (args, format); vfprintf (stream, format, args); vprintf (format, args); va_end (args); } //打开文件句柄 void fOpenFileFp(FILE** fp, int flag)//标志,1:文件名用时间命名,2:指定命令 { if (NULL == *fp) { *fp = (FILE*)malloc(sizeof(FILE)); if (NULL == *fp) { printf("malloc error!!!\n"); return; } } char strTemFileName[80]; GetCurSysTime(strTemFileName); strcat(strTemFileName,".log"); strcpy(g_strTemFileName,strTemFileName); if (FLAG_FILENAME_TIME == flag && (*fp = fopen(g_strTemFileName, "a")) == NULL) { WriteFormatted(g_fpLog,"Can't open %s \n", g_strTemFileName); exit(-1); } else if(FLAG_FILENAME_ZHID == flag && (*fp = fopen("OutPut.txt", "a")) == NULL) { WriteFormatted(g_fpLog,"Can't open %s \n", g_strTemFileName); exit(-1); } } /* 初始化全局变量 */ void InitGlobalVar() { g_lCheckFileNumber = 0; g_lCheckOkFileNumber = 0; g_lClearFileNumber = 0; g_flagDeleteFile = FLAG_VIEW_FILE; g_fpLog = NULL; g_fpCheckOK = NULL; /* 初始化全局变量 fp */ if (NULL == g_fpLog) { fOpenFileFp(&g_fpLog,FLAG_FILENAME_TIME); } /* 初始化全局变量 fp */ if (NULL == g_fpCheckOK) { fOpenFileFp(&g_fpCheckOK,FLAG_FILENAME_ZHID); } /* 初始化全局变量 */ GetCurSysTime( g_pStrTime ); strcpy(g_strFindFilePath,FIND_FILE_PATH); strcpy(g_strFindFileType,FIND_FILE_TYPE); g_iCount = 0; } void FreePTime(PSYSTEMTIME pTime) { /* 一定要进行下面判断,否则会造成重复释放NULL地址而崩溃 */ if (NULL != pTime) { free(pTime); pTime = NULL; } } //打开文件句柄 void fCloseFileFp(FILE** fp) { if (*fp) { fclose(*fp); *fp = NULL; } } //对全局的指针进行释放,并指向NULL void CloseOrFreeGlobalVar() { fCloseFileFp(&g_fpLog); fCloseFileFp(&g_fpCheckOK); } /* Modify by zengwenwu @ 2012-3-19 :增加一标志,提供不同打印要求 */ //1:打印长时间 //0:短时间 //-1:不打印,相当于只对pTime做赋值 void GetCurSysTime(char* str) { LPSYSTEMTIME pstTemSysTime = NULL; pstTemSysTime = (LPSYSTEMTIME)malloc( sizeof(SYSTEMTIME) ); if ( NULL == pstTemSysTime ) { WriteFormatted(g_fpLog,"SYSTEMTIME malloc is err\n" ); return; } memset(pstTemSysTime,0,sizeof(LPSYSTEMTIME)); GetLocalTime( pstTemSysTime ); sprintf((char*)str,"%04d-%02d-%02d %02d_%02d_%02d %03d", pstTemSysTime->wYear, pstTemSysTime->wMonth, pstTemSysTime->wDay, pstTemSysTime->wHour, pstTemSysTime->wMinute, pstTemSysTime->wSecond, pstTemSysTime->wMilliseconds ); } //Function: // Visit all folders and files // //Paremeter: // char *lpPath -- path of file // //Return: // void // void VisitAllFiles(char * lpPath) { char szFind[MAX_PATH]; WIN32_FIND_DATA FindFileData; char chTemPath[MAX_FIND_PATH]; strcpy(chTemPath,lpPath); strcpy(szFind,lpPath); strcat(szFind,"\\*.*"); /* 对全局时间g_pTime变量赋值 */ if (0 == g_iCount++) { WriteFormatted(g_fpLog,"查找%s下%s类型的文件\n",g_strFindFilePath,g_strFindFileType); WriteFormatted(g_fpLog,"BEGIN-------------------------------------------------%s\n",g_pStrTime); WriteFormatted(g_fpCheckOK,"%s\n","\n\n////////////////////////////////////////////////////////////////"); WriteFormatted(g_fpCheckOK,"查找目录%s下%s类型的文件\n",g_strFindFilePath,g_strFindFileType); WriteFormatted(g_fpCheckOK,"BEGIN-------------------------------------------------%s\n",g_pStrTime); } /*printf("+------进入目录--->\n+[%s]\n\r",chTemPath);*/ WriteFormatted(g_fpLog,"+------进入目录--->\n+[%s]\n",chTemPath); HANDLE hFind=::FindFirstFile(szFind,&FindFileData); if(INVALID_HANDLE_VALUE == hFind) { WriteFormatted(g_fpLog,"<%s>No %s file found\n",g_pStrTime,g_strFindFileType); return; } while(TRUE) { //If director, visit all sub-folders if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { g_lCheckFileNumber++; if(FindFileData.cFileName[0]!='.') { char szFile[MAX_PATH]; strcpy(szFile,lpPath); strcat(szFile,"\\"); /*strcat(chTemPath,szFile);*/ strcat(szFile,FindFileData.cFileName); VisitAllFiles(szFile); } } else { //Judge if TYPE file g_lCheckFileNumber++; /*printf(" -%s\n",FindFileData.cFileName);*/ int len = strlen(FindFileData.cFileName); int iStrLen = strlen(g_strFindFileType); //除去目录,共检查文件数 const char *p = (char *)&FindFileData.cFileName[len-iStrLen]; //case insentive! if ((_stricmp(p, g_strFindFileType) == 0)) { g_lCheckOkFileNumber++; //if exe file, check it char strFileName[MAX_PATH]; strcpy(strFileName,lpPath); strcat(strFileName,"\\"); strcat(strFileName,FindFileData.cFileName); WriteFormatted(g_fpLog,"<%s>已检查文件数:%ld,符合条件数:%ld,刚检查到的文件是:\n%s\n", g_pStrTime, g_lCheckFileNumber, g_lCheckOkFileNumber, strFileName); fprintf(g_fpCheckOK,"<%s> %s\n", g_pStrTime, strFileName); /* 置删除标志时,删除文件 */ /*if (CheckWHVirus(fp))*/ if (g_flagDeleteFile == FLAG_DELETE) { /*PrintLocalTime(g_pStrTime);*/ /*ClearVirus(fp, strFileName);*/ //fp closed in the function /*printf("Virus Found! %s and cleared\r\n", strFileName);*/ /* 去只读 */ SetFileAttributes(strFileName,FILE_ATTRIBUTE_NORMAL); /* Long,非零表示成功,零表示失败。会设置GetLastError */ if (DeleteFile(strFileName)) { g_lClearFileNumber++; /*printf("Delete file: %s successfully,已清除文件数:%ld.\n", strFileName,g_lClearFileNumber);*/ WriteFormatted(g_fpLog,"<%s>---Delete file: %s successfully,已清除文件数:%ld.\n", g_pStrTime,strFileName,g_lClearFileNumber); } else { /*printf("Delete file: %s failed!\n", strFileName);*/ WriteFormatted(g_fpLog,"<%s>---Delete file: %s failed!\n", g_pStrTime,strFileName); } } /*system("cls");*/ /*Sleep(SLEEP_SECOND);*/ } } //Find next file if(!FindNextFile(hFind,&FindFileData)) { // printf("【OK,There is not %s type file in PATH %s】\n",TYPE,chTemPath); // fprintf(g_fpLog,"---------------->【OK,There is not %s type file any more in PATH %s】\n",TYPE,chTemPath); break; } } /*GetCurSysTime(g_pStrTime);*/ /*fprintf(g_fpLog,"END---------------------------------------------------%s\n",g_pStrTime);*/ FindClose(hFind); } //从配置文件中读取配置信息: //PATH:需要进行查找/删除的路径名 //TYPE:需要进行查找的文件类型名称 //DORV:是删除还是查看:0查看,1删除 void ReadConfigFromFile() { FILE * fpConf = NULL; char strPathTemp[100]; char strTypeTemp[10]; /*char strCurTime[50];*/ int flagDelOrView = 0; char strCfgFilePath[100]; memset(strPathTemp,0,sizeof(strPathTemp)); memset(strTypeTemp,0,sizeof(strTypeTemp)); memset(strCfgFilePath,0,sizeof(strTypeTemp)); GetCurrentDirectory(MAX_FIND_PATH,strCfgFilePath); if (NULL == (fpConf = fopen("config.cfg","r"))) { WriteFormatted(g_fpLog,"Read config file error!!!->%s\n",strCfgFilePath); printf("获取当前路径为:%s\n",strCfgFilePath); strcat(strCfgFilePath,"\\"); strcat(strCfgFilePath,"config.cfg"); if (NULL == (fpConf = fopen(strCfgFilePath,"r"))) { WriteFormatted(g_fpLog,"Read config file error!!!->%s\n",strCfgFilePath); exit(0); } } GetCurSysTime(g_pStrTime); fscanf(fpConf,"PATH=%s\n",strPathTemp); fscanf(fpConf,"TYPE=%s\n",strTypeTemp); fscanf(fpConf,"DORV=%d\n",&flagDelOrView); WriteFormatted(g_fpLog,"<%s>读取的文件配置信息:\n",g_pStrTime); WriteFormatted(g_fpLog,"PATH=%s\n",strPathTemp); WriteFormatted(g_fpLog,"TYPE=%s\n",strTypeTemp); WriteFormatted(g_fpLog,"DORV=%d\n",flagDelOrView); if (!(strcmp(strPathTemp,"") && strcmp(strTypeTemp,""))) { printf("配置为空,请修改配置文件\n"); exit(-1); } strcpy(g_strFindFilePath,strPathTemp); strcpy(g_strFindFileType,strTypeTemp); g_flagDeleteFile = flagDelOrView; WriteFormatted(g_fpLog,"<%s>全局配置信息:\n",g_pStrTime); WriteFormatted(g_fpLog,"g_strFindFilePath=%s\n",g_strFindFilePath); WriteFormatted(g_fpLog,"g_strFindFileType=%s\n",g_strFindFileType); WriteFormatted(g_fpLog,"g_flagDeleteFile=%d\n",g_flagDeleteFile); fCloseFileFp(&fpConf); WriteFormatted(g_fpLog,"Read config successfully!!!\n"); Sleep(3000); } //保存配置到配置文件中: //PATH:需要进行查找/删除的路径名 //TYPE:需要进行查找的文件类型名称 //DORV:是删除还是查看:0查看,1删除 void SaveConfigToFile() { FILE * fpConf = NULL; char strPathTemp[100]; char strTypeTemp[10]; /*char strCurTime[50];*/ int flagDelOrView = 0; WriteFormatted(g_fpLog,"<%s>保存配置信息:\n",g_pStrTime); memset(strPathTemp,0,sizeof(strPathTemp)); memset(strTypeTemp,0,sizeof(strTypeTemp)); if (NULL == (fpConf = fopen("config.cfg","w"))) { WriteFormatted(g_fpLog,"Read config file error !!!\n"); exit(-1); } if (!(strcmp(g_strFindFilePath,"") && strcmp(g_strFindFileType,""))) { WriteFormatted(g_fpLog,"配置为空,请重新配置\n"); exit(-1); } //将配置信息打印出来 //保存到配置文件中 /*fprintf(g_fpLog,"<%s>全局配置信息:\n",g_pStrTime);*/ fprintf(fpConf,"PATH=%s\n",g_strFindFilePath); fprintf(fpConf,"TYPE=%s\n",g_strFindFileType); fprintf(fpConf,"DORV=%d\n",g_flagDeleteFile); //将保存后的信息写到日志及标准输出中 WriteFormatted(g_fpLog,"<%s>保存配置后的配置信息:\n",g_pStrTime); WriteFormatted(g_fpLog,"PATH=%s\n",g_strFindFilePath); WriteFormatted(g_fpLog,"TYPE=%s\n",g_strFindFileType); WriteFormatted(g_fpLog,"DORV=%d\n",g_flagDeleteFile); fCloseFileFp(&fpConf); WriteFormatted(g_fpLog,"Save config successfully!!!\n"); Sleep(3000); } void ModifyCfg() { WriteFormatted(g_fpLog,"////////////////////////////////////////////////////////////////\n"); WriteFormatted(g_fpLog,"%s\n","修改配置文件:"); WriteFormatted(g_fpLog,"////////////////////////////////////////////////////////////////\n"); printf("请输入路径:"); scanf("%s",g_strFindFilePath); printf("请输入需要查找的文件类型:"); scanf("%s",g_strFindFileType); if(!strcmp("",g_strFindFilePath) || !strcmp("",g_strFindFileType)) { WriteFormatted(g_fpLog,"竟然不输入路径和类型,想搞死我啊!!!\n"); /*GetCurrentDirectory(MAX_FIND_PATH,g_strFindFilePath);*/ // memset(g_strFindFileType,0,10); // strcpy(g_strFindFileType,"log"); ReadConfigFromFile(); } printf("请输入是查看还是删除:0:查看,1:删除\n"); scanf("%d",&g_flagDeleteFile); if ((g_flagDeleteFile != FLAG_DELETE) && (g_flagDeleteFile != FLAG_VIEW_FILE)) { g_flagDeleteFile = FLAG_DELETE; } WriteFormatted(g_fpLog, "g_strFindFilePath:%s\n,g_strFindFileType:%s\n,g_flagDeleteFile:%d\n", g_strFindFilePath, g_strFindFileType, g_flagDeleteFile); //保存配置到配置文件中 SaveConfigToFile(); /*system("dir");*/ } int main(int argc,char* argv[]) { printf("----------------------------------------------\n"); printf("DeleteFile.exe (argv[1]) (argv[2]) (argv[3]) :\n"); printf("argv[1]:绝对路径名(最后不加\\\n"); printf("argv[2]:要查找or删除的文件类型\n"); printf("argv[3]:标志:0--查看 1--删除\n"); if (argc < 2) { char ch; while(1) { printf("=========================================================\n"); printf("兄弟,输入的参数太少了,你需要自己输入参数,知道不!!!\n"); printf("自己输入:y,自己不想输入:<任意键>\n"); ch = getch(); putchar(ch); printf("\n"); InitGlobalVar(); if ('y' == ch || 'Y' == ch) { ModifyCfg(); printf("即将要查找%s下%s类型的文件了哦\n",g_strFindFilePath,g_strFindFileType); Sleep(3000); VisitAllFiles(g_strFindFilePath); CloseOrFreeGlobalVar(); } else if (ch =='q' || ch == 'e' || ch == 'Q' || ch == 'q') { CloseOrFreeGlobalVar(); break; } else if (ch == 'm') { ModifyCfg(); } else { printf("太懒了,这个是用做测试用的,大哥\n"); Sleep(2000); ReadConfigFromFile(); VisitAllFiles(g_strFindFilePath); CloseOrFreeGlobalVar(); system("OutPut.txt"); } printf("<Enter to continue>\n"); getch(); system("cls"); } } else { InitGlobalVar(); if (argc == 2) { strcpy(g_strFindFilePath,argv[1]); VisitAllFiles(g_strFindFilePath); } else if(argc == 3) { strcpy(g_strFindFilePath,argv[1]); strcpy(g_strFindFileType,argv[2]); VisitAllFiles(g_strFindFilePath); } else if (argc == 4) { strcpy(g_strFindFilePath,argv[1]); strcpy(g_strFindFileType,argv[2]); g_flagDeleteFile = atoi(argv[3]); VisitAllFiles(g_strFindFilePath); } else { WriteFormatted(g_fpLog,"兄弟,输入的参数太多了,欠抽啊!!!\n"); } CloseOrFreeGlobalVar(); } /*(void)DeleteDirFile(FILE_PATH, FILE_NAME_TYPE,FLAG_VIEW_FILE);*/ /*(void)FindFileInDirWithType(FILE_PATH, FILE_NAME_TYPE,FLAG_VIEW_FILE);*/ /* 用.txt关联的程序打开,这里需要先关闭 FILE * fp 后才能打开 */ /*system((const char*)g_strTemFileName);*/ // // fCloseFileFp(&g_fpLog); // fCloseFileFp(&g_fpCheckOK); // FreePTime(g_pTime); return 0; }
运行效果:
查找效果图:
生成的日志文件以时间命名,以及查找输出OutPut.txt: