VC++实现文件全盘搜索
很久没更新了,今天决定写点东西。
这是我以前写的一个C++的类,可以在硬盘上全盘搜索指定的文件(可以用通配符),还可以添加过滤器,以便搜索特定的文件。该类使用链表储存搜索结果(学了那么多数据结构,总算可以用一把了),性能还是可以的。虽说没什么技术含量,但也挺有用的
下面就是这个类的代码,包括测试程序,原本我没有写注释,现在加上了一些。
这个博客居然不支持C++的代码插入,只好用C#的来将就一下了.

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