標籤:建立 [] 刪除 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下檔案尋找