首页 > 其他 > 详细

VC/MFC 下 递归遍历目录下的所有子目录及文件

时间:2014-02-28 08:32:42      阅读:755      评论:0      收藏:0      [点我收藏+]

在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

VC/MFC 下 递归遍历目录下的所有子目录及文件

原文:http://www.cnblogs.com/JczmDeveloper/p/3571438.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!