#include <windows.h> #include <shlwapi.h> #include <iostream> #pragma comment(lib,"shlwapi.lib") using namespace std; //定义过滤器的最大数量 #define CONST_MAX_FILTER 16 //链表的数据结构 typedef struct tagList{ TCHAR szFile[MAX_PATH]; struct tagList *NextFile; }FileList, *PFileList; //主体类 class CHunter{ public: CHunter(); ~CHunter(); void AddFilter( TCHAR *szFilter );//添加过滤器 void CHunter::Hunt( TCHAR *szPath ); TCHAR *GetFile();//取得链表中的文件 DWORD GetFileCount();//取得文件的数量 private: PFileList headNode;//链表头 PFileList currNode; void AddFile( TCHAR *szFile ); void HuntFile( char *lpPath ) ; TCHAR szFilter[CONST_MAX_FILTER][5] ; DWORD dwFilterCount ; DWORD dwFileCount ; }; CHunter::CHunter():dwFilterCount(0),dwFileCount(0) { headNode = (FileList *)malloc( sizeof(FileList) ); headNode->NextFile = NULL; currNode = headNode; for(int i=0; i< CONST_MAX_FILTER; i++) ZeroMemory( szFilter[i], 5 ) ; } CHunter::~CHunter() { PFileList next, tmp; tmp = headNode; while( tmp->NextFile != NULL ) { next = tmp->NextFile ; free(tmp); tmp = next; } free(tmp); } //添加过滤器,比如.txt,为了简省,没有考虑文件扩展名长度大于4的情况 //请自行修改 void CHunter::AddFilter( TCHAR *szInp ) { if( strlen(szInp) > 4 ) return; strncpy( szFilter[dwFilterCount++], szInp, 5 ); } void CHunter::AddFile( TCHAR *szFile ) { currNode->NextFile = (FileList *)malloc( sizeof(FileList) ); currNode = currNode->NextFile; ZeroMemory(currNode->szFile, MAX_PATH ); currNode->NextFile = NULL ; strncpy( currNode->szFile, szFile, MAX_PATH ); dwFileCount++; } //这是入口函数,调用它即可开始搜索,这个路径的尾部不应有反斜杠 void CHunter::Hunt( TCHAR *szPath ) { HuntFile( szPath ); currNode = headNode->NextFile; } DWORD CHunter::GetFileCount() { return this->dwFileCount; } //这个函数依次遍历链表中的文件,并返回一个文件名 TCHAR *CHunter::GetFile() { TCHAR *szRet; szRet = currNode->szFile; currNode = currNode->NextFile; return szRet; } void CHunter::HuntFile(char * lpPath) { char szFind[MAX_PATH]; char szFile[MAX_PATH]; WIN32_FIND_DATA FindFileData; ZeroMemory(szFind,MAX_PATH); ZeroMemory(szFile,MAX_PATH); strcpy(szFind,lpPath); strcat(szFind,"\\*.*"); HANDLE hFind=::FindFirstFile(szFind,&FindFileData); if(INVALID_HANDLE_VALUE == hFind) return; while(TRUE) { if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if(FindFileData.cFileName[0]!='.') { strcpy(szFile,lpPath); strcat(szFile,"\\"); strcat(szFile,FindFileData.cFileName); HuntFile(szFile); } } else { strcpy(szFile,lpPath); strcat(szFile,"\\"); strcat(szFile,FindFileData.cFileName); //cout << szFile << FindFileData.cFileName << endl; for( int i=0; i< dwFilterCount; i++ ) if( strncmp(szFilter[i], PathFindExtension(szFile), 5) == 0 ) this->AddFile( szFile ); } if(!FindNextFile(hFind,&FindFileData)) break; } FindClose(hFind); } //示例,搜索D盘所有的exe文件 int main(int argc, char* argv[]) { CHunter hunter; hunter.AddFilter(".exe"); hunter.Hunt("D:"); cout<< hunter.GetFileCount() << endl; for(int i=0; i< hunter.GetFileCount(); i++) cout<< hunter.GetFile() << endl; return 0; }