windows下檔案尋找

來源:互聯網
上載者:User

標籤:建立   []   刪除   ace   今天   tchar   ref   object   return   

今天再看樹的結構時,想起檔案檔案的儲存就是一個典型的樹結構,因此基於MFC提供的函數,寫了一個關於檔案尋找的代碼。

  • 因為需要用到MFC的類,所以在建立控制台項目時,需要關聯MFC;
  • 該代碼刪除了由於關聯MFC而產生的一些其他代碼,保留_tmain()函數即可;
  • 採用寬字元,因此使用了wcout輸出

功能:

  收縮指定磁碟中的檔案,可以進行簡單的模糊尋找。

 1 #include "stdafx.h" 2 #include "ListWindowsFile.h" 3  4 #ifdef _DEBUG 5 #define new DEBUG_NEW 6 #endif 7  8  9 using namespace std;10 11 int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])12 {13     int nRetCode = 0;14     setlocale(LC_ALL, "chs");15         //尋找F盤中檔案名稱中含有RewindBoard的檔案16     CString path = TEXT("F:\\");17     CString name = TEXT("RewindBoard");18     FindFileWindows(path, name);19     system("pause");20         return nRetCode;21 }22 23 void FindFileWindows(CString BasePath, CString object)24 {25     CFileFind Finder;26     CString PreFilePath;27     CString NextFullName;28     CString FileName;29 30     PreFilePath = BasePath + TEXT("*.*");31     int iResult = Finder.FindFile(PreFilePath);  //遍曆整個檔案夾32 33     while (0 != iResult)34     {35         iResult = Finder.FindNextFileW();36 37         //判斷是否為檔案夾38         if(Finder.IsDirectory() && !Finder.IsDots())39         {40             NextFullName = Finder.GetFilePath();41             FileName = Finder.GetFileName();42             if (FileName.Find(object, 0) != -1)43             {44                 std::wcout << (LPCTSTR)Finder.GetFilePath() << std::endl;45             }46             NextFullName += TEXT("\\");47             FindFileWindows(NextFullName, object);48         }49         else  //尋找該目錄中是否有該檔案50         {51             FileName = Finder.GetFileName();52             if (FileName.Find(object, 0) != -1)53             {54                 std::wcout << (LPCTSTR)Finder.GetFilePath() << std::endl;55             }56         }57     }58 }

 

運行結果:

 

windows下檔案尋找

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.