win32文件搜索

Code:
  1. #include <windows.h>   
  2.   
  3. void SeachFile(TCHAR *lpDirectory, TCHAR *lpFileName);   
  4. void FindFile(TCHAR *lpDirectory, TCHAR *lpFileName);   
  5. typedef struct _DirList   
  6. {   
  7.     TCHAR Diretory[MAX_PATH];   
  8.     struct _DirList *lpNext;   
  9. }DirList;   
  10.   
  11. DirList *first = NULL;   
  12. DirList *last = NULL;   
  13. DirList *newList = NULL;   
  14.   
  15. // 把新的文件夾路徑加入到鏈表中    
  16. void AddList(TCHAR  *lpDiretory)   
  17. {   
  18.   
  19.     newList = new DirList;   
  20.     wsprintf(newList->Diretory, "%s", lpDiretory);   
  21.     newList->lpNext = NULL;   
  22.     if (first == NULL)   
  23.     {   
  24.         first = newList;   
  25.         last = newList;   
  26.     }   
  27.   
  28.     else  
  29.     {   
  30.         last->lpNext = newList;   
  31.         last = newList;   
  32.     }   
  33.        
  34. }   
  35.   
  36. int main(int argc, char* argv[])   
  37. {   
  38.     printf("Hello World!/n");   
  39.     SeachFile("d:""main.c");   
  40.     return 0;   
  41. }   
  42.   
  43. void SeachFile(TCHAR *lpDirectory, TCHAR *lpFileName)   
  44. {   
  45.     DirList NewList;   
  46.     wsprintf(NewList.Diretory, "%s", lpDirectory);   
  47.     NewList.lpNext = NULL;   
  48.     last = &NewList;   
  49.     first = &NewList;   
  50.   
  51.     while(TRUE)   
  52.     {   
  53.         DirList *PFind;   
  54.         if (first != NULL)   
  55.         {   
  56.             PFind = first;   
  57.             first = first->lpNext;   
  58.             FindFile(PFind->Diretory, lpFileName);   
  59.         }   
  60.   
  61.         else  
  62.         {   
  63.             TCHAR ShowMsg[256] = "問件搜索結束!!/n";   
  64.             DWORD dWriteFile;   
  65.         WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), ShowMsg, strlen(ShowMsg), &dWriteFile, FALSE);   
  66.             return ;   
  67.         }   
  68.     }   
  69.   
  70.     return ;   
  71. }   
  72.   
  73. // 查找文件。把找到的文件夾放入鏈表   
  74. void FindFile(TCHAR *lpDirectory, TCHAR *lpFileName)   
  75. {   
  76.     TCHAR FileRoad[MAX_PATH];   
  77.     TCHAR DirRoad[MAX_PATH];   
  78.     TCHAR FindedFile[MAX_PATH];   
  79.     TCHAR FindedDir[MAX_PATH];   
  80.     ZeroMemory(FileRoad, sizeof(FileRoad));   
  81.     ZeroMemory(DirRoad, sizeof(DirRoad));   
  82.     ZeroMemory(FindedFile, sizeof(FindedFile));   
  83.     ZeroMemory(FindedDir, sizeof(FindedDir));   
  84.   
  85.     // 找到lpDirectory下的文件夾加入鏈表   
  86.     wsprintf(DirRoad, "%s//*.*", lpDirectory);   
  87.     WIN32_FIND_DATA findData;   
  88.     HANDLE hFile;   
  89.     hFile = FindFirstFile(DirRoad, &findData);   
  90.     if (hFile != INVALID_HANDLE_VALUE)   
  91.     {   
  92.         do  
  93.         {   
  94.             // 如果是文件夾就加入鏈表   
  95.             if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)   
  96.             {   
  97.                 ZeroMemory(DirRoad, sizeof(DirRoad));   
  98.                 wsprintf(DirRoad, "%s//%s", lpDirectory, findData.cFileName);    
  99.                 // 加入文件夾鏈表   
  100.                 AddList(DirRoad);   
  101.             }   
  102.   
  103.             else  
  104.             {   
  105.                 continue;   
  106.             }   
  107.         }while(FindNextFile(hFile, &findData));   
  108.     }   
  109.   
  110.     // 找到lpDirectory下的以lpFileName爲格式的文件   
  111.   
  112.     wsprintf(FileRoad, "%s//%s", lpDirectory, lpFileName);   
  113.     hFile = FindFirstFile(FileRoad, &findData);   
  114.     DWORD dWriteFile;   
  115.     if (hFile != INVALID_HANDLE_VALUE)   
  116.     {   
  117.         do  
  118.         {   
  119.             ZeroMemory(FileRoad, sizeof(FileRoad));   
  120.             wsprintf(FileRoad, "%s//%s/n", lpDirectory, findData.cFileName);   
  121.             WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), FileRoad, strlen(FileRoad), &dWriteFile, FALSE);   
  122.         }while(FindNextFile(hFile, &findData));   
  123.     }   
  124. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章