MFC遍历文件夹里的文件

遍历文件夹

关键函数--递归法

 

Code
   
     
1
2   void CFileSearchDlg::TraverseFolder( const CString & strDir,std::vector < CString >& vecFile )
3 {
4 WIN32_FIND_DATA FindFileData;
5
6 CString strDirTmp;
7 strDirTmp = strDir;
8 strDirTmp += " \\*.* " ;
9
10 HANDLE hFind = ::FindFirstFile(strDirTmp, & FindFileData);
11 if (INVALID_HANDLE_VALUE == hFind)
12 {
13 return ;
14 }
15
16 while (TRUE)
17 {
18 if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
19 {
20 if (FindFileData.cFileName[ 0 ] != _T( ' . ' ))
21 {
22 strDirTmp = strDir;
23 strDirTmp += " \\ " ;
24 strDirTmp += FindFileData.cFileName;
25 TraverseFolder(strDirTmp,vecFile) ;
26 }
27 }
28 else
29 {
30 strDirTmp = strDir;
31 strDirTmp += " \\ " ;
32 strDirTmp += FindFileData.cFileName;
33 vecFile.push_back(strDirTmp);
34 }
35 if ( ! FindNextFile(hFind, & FindFileData)) break ;
36 }
37 FindClose(hFind);
38
39 }

 

 

 

你可能感兴趣的:(遍历文件夹)