windows编程使用递归方法遍历文件的方法

来源:互联网 发布:亚马逊运营书籍知乎 编辑:程序博客网 时间:2024/05/20 19:32

在论坛经常碰到初学者不知道如何遍历文件,其实很简单,只要使用下面的代码就可以了。

void ChkRecurse(LPCTSTR pstr /* = NULL */){    CFileFind   finder;       //   build   a   string   with   wildcards       CString   strWildcard(pstr);       strWildcard   +=   _T("\\*.*");           //   start   working   for   files       BOOL   bWorking   = FALSE;    try    {        bWorking = finder.FindFile(strWildcard);       }    catch (CException* e)    {        char szCause[255] = {0};        e->GetErrorMessage(szCause, 255);        Log->LogOutEx(szCause);            }        while   (bWorking)       {           try        {            bWorking   =   finder.FindNextFile();               //   skip   .   and   ..   files;   otherwise,   we'd               //   recur   infinitely!               if   (finder.IsDots()) // '.' and '..'                continue;            if  (finder.IsSystem())// 系统文件                continue;            else if   (finder.IsDirectory()) //目录              {                   CString   str   =   finder.GetFilePath();                   CString      str1;                GetDirName(str,str1);                if ( str1.GetLength() == 12  && _is_num(str1) )                {                    ChkRecurse(str);//回调                }                else                {                    Sleep(20);                    continue;                }            }               else            {//检查文件                         //Do your work...                        }        }        catch (CException* e)        {            char szCause[255] = {0};            e->GetErrorMessage(szCause, 255);            Log->LogOutEx(szCause);            }    }        finder.Close();   }

有不明白的地方欢迎评论、留言。

原创粉丝点击