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

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

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();   
}

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

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值