[转]使用FindFirstFile,FindNextFile遍历一个文件夹

原文地址:http://www.cnblogs.com/chenkunyun/archive/2012/03/24/2415727.html

 
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
//遍历文件夹函数
void TraverseFolder(LPCTSTR lpPath)
{
    TCHAR szFind[MAX_PATH] = {_T("\0")};
    WIN32_FIND_DATA findFileData;
    BOOL bRet;
 
    _tcscpy_s(szFind, MAX_PATH, lpPath);
    _tcscat_s(szFind, _T("\\*.*"));     //这里一定要指明通配符,不然不会读取所有文件和目录
 
    HANDLE hFind = ::FindFirstFile(szFind, &findFileData);
    if (INVALID_HANDLE_VALUE == hFind)
    {
        return;
    }
 
    //遍历文件夹
    while (TRUE)
    {
        if (findFileData.cFileName[0] != _T('.'))
        {//不是当前路径或者父目录的快捷方式
            _tprintf(_T("%s\\%s\n"), lpPath, findFileData.cFileName);
            if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {//这是一个普通目录
                //设置下一个将要扫描的文件夹路径
                _tcscpy_s(szFind, MAX_PATH, lpPath);   
                _tcscat_s(szFind, _T("\\"));   
                _tcscat_s(szFind, findFileData.cFileName);
                ///_tcscat_s(szNextDir, _T("\\*"));
                //遍历该目录
                TraverseFolder(szFind);
            }
        }
        //如果是当前路径或者父目录的快捷方式,或者是普通目录,则寻找下一个目录或者文件
        bRet = ::FindNextFile(hFind, &findFileData);
        if (!bRet)
        {//函数调用失败
            //cout << "FindNextFile failed, error code: "
            //  << GetLastError() << endl;
            break;
        }
    }
 
    ::FindClose(hFind);
}
 

 

posted on 2015-01-05 10:36  sou1boy  阅读(137)  评论(0)    收藏  举报

导航