在MFC下要实现文件夹的递归遍历,可用CFileFind类,依次读取文件夹下的子文件夹和文件,并判断通过判断是文件夹还是文件来决定递归遍历。递归遍历代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 |
/************************************************************************/ /* 遍历打包目录下的所有文件 */ /************************************************************************/ void
CCopyFileCheckerDlg::FindFileInDir(CString rootDir) { // 查找当前路径下的所有文件夹和文件 CString strDir = rootDir; strDir += "\\*.*" ; // 遍历得到所有子文件夹名 CFileFind finder; BOOL
bWorking = finder.FindFile(strDir); while
(bWorking) { bWorking = finder.FindNextFile(); if
(finder.IsDirectory() && "."
!= finder.GetFileName() && ".."
!= finder.GetFileName()) //注意该句需要排除“.”“..” { //递归调用 if (finder.GetFileName() != L "源PE文件" ) FindFileInDir(finder.GetFilePath()); } else { CString strFile = finder.GetFilePath(); CString strFileName = finder.GetFileName(); CString strFileExtension = GetFileExtension(strFileName); if (L "."
!= strFileName&&L ".."
!= strFileName) { if (strFileExtension.CompareNoCase(L "exe" ) == 0 ||strFileExtension.CompareNoCase(L "dll" )==0) { if (IsNeedCopy(strFileName,strFile)) { m_nPEFileNum ++; INT
nRow = m_listResult.InsertItem(m_nCount, strFile); //插入行 m_listResult.SetItemText(nRow, 1, strFile); //设置数据 m_listResult.SetCheck(nRow,TRUE); m_progress.SetPos(m_nCount); } } } } } finder.Close(); } |
VC/MFC 下 递归遍历目录下的所有子目录及文件,布布扣,bubuko.com
原文:http://www.cnblogs.com/JczmDeveloper/p/3571438.html