CFileFind遍历文件

本文介绍了一个C++函数getFolderFile,该函数用于遍历指定文件夹下的所有文件,并根据配置文件中的文件名列表来更新二维码内容。具体实现包括了文件查找、文件名匹配及二维码信息更新等步骤。

int CAT_OLC_DUTDlg::getFolderFile(CString pathStr) 
{
    CString myDataPath, fdPath;   //设置路径变量
    myDataPath = pathStr + L"\\*.*"; //文件夹路径  路径后面加*.*就会遍历该路径目录下的所有文件

    CFileFind find;        //例化CFileFind
    BOOL bf = find.FindFile(myDataPath);    
    int a = 0;
    while (bf)
    {
        
        bf = find.FindNextFile();
        if (!find.IsDots())
        {
            CString nowfilename = find.GetFileName();//这一句要放在isDots之后。isDots是判断当前文件名是否是.或..
            if (find.IsDirectory())
            {
                //如果是文件夹,跳过                      
                continue;
            }
            else
            {
                    
                    //CreateTeatLog(_T("findfilename:")+ nowfilename);
                    CString fn = fileNames;//fileNames全局变量,配置文件里获取并赋值,包含所有需要查找的文件名的字串
                    std::string conName = (CStringA)fn;//转换为string好使用find方法 CString的Find只能找到单个字符
                    std::string nowFindName = (CStringA)nowfilename;
                    if (conName.find(nowFindName) != conName.npos) {//匹配到配置中的文件名
                        //匹配对应文件名更新二维码信息
                        int pos;
                        pos = nowFindName.find(".");
                        string strs = nowFindName.substr(0,pos );
                        CString prefixName = CA2T(strs.c_str());
                        if (HeadBarcode.Find(prefixName) < 0) {//判断二维码内容中是否已经有该信息,已经有该信息则不重复更新
                            HeadBarcode += prefixName + _T("=Pass$");
                            //输出log用于验证
                            CreateTeatLog(_T("QR code update content:")+ prefixName+ _T("=Pass$"));
                            //在更新二维码前清空显示内容控件 再显示更新后的内容
                            m_Edit.SetSel(0, -1);
                            m_Edit.ReplaceSel(_T(""));
                            Prepare2DBarcode(requestpngpath, imagename1, HeadBarcode);
                            m_Edit.SetSel(-1, 1);
                            m_Edit.ReplaceSel(_T("[Request]\r\n"));
                            m_Edit.ReplaceSel(HeadBarcode);
                            SetTimer(2, 100, 0);
                            a = 1;
                        }
                    }
                
            }
        }
    }
    find.Close();
    return a;
}

void CAT_OLC_DUTDlg::Prepare2DBarcode(CString FileName,CString imgname,CString STR )
{
    STR.Trim();
    QRGenerator(STR);
}

/** ===================================================== 功能: CFolderContent类搜索一个目录, 列出该目录下的所有目录名称,列出所有子目录下的文件名称等属性。 作者: jef 作者邮箱: dungeonsnd@126.com 发步时间: 20100311 版本: v1.1 版权: 请遵循GNU. 对外接口: int GetAllSub(CString csPath) csPath: 一个目录或者一个完整的文件名 使用举例: void CGetFolderContentView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CString cs,csSaveFileName,csFileSave,csT,csT1; cs ="C:\\Documents and Settings\\All Users\\Documents\\My Music"; char chModule[8192]; memset(chModule,0,8192); GetModuleFileName(NULL,chModule,8192); csT.Format("%s",chModule); csT =csT.Left( csT.ReverseFind(_T('\\')) ); csSaveFileName =csT+_T("\\FolderContent输出文件.txt"); CRect rtClient; GetClientRect(&rtClient); CClientDC dc(this); dc.SetTextColor(RGB(200,80,80)); dc.FillSolidRect(rtClient,RGB(240,240,240)); CFolderContent fc; fc.GetAllSub(cs); int i,len,k; csT ="-------"; csT =cs+"总大小为"; csT1.Format(" %.4fMB,清单文件已保存在 %s",fc.m_dTotalSize/double(1024*1024),csSaveFileName); csT +=csT1; csT +="-------"; dc.TextOut(5,0,csT); csFileSave +=csT+_T("\r\n"); csT ="-------"; csT +="所有子目录如下:"; csT +="-------"; dc.TextOut(5,25,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_Directory.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecDirectorySize[i]/double(1024*1024)); csT1.Format(" 创建时间:%d年%d月%d日 ", fc.m_vecFileLastCreationTime[i].wYear, fc.m_vecFileLastCreationTime[i].wMonth, fc.m_vecFileLastCreationTime[i].wDay); csT =csT+csT1; csT =fc.m_Directory.GetAt(i)+csT; dc.TextOut(5,(i+2)*20,csT); csFileSave +=csT+_T("\r\n"); } k =i; csT ="-------"; csT +="所有文件如下:"; csT +="-------"; dc.TextOut(5,(k+3)*20,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_PathNameExt.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecFileSize[i]/double(1024*1024)); csT1.Format(" 修改时间:%d年%d月%d日 ", fc.m_vecFileLastWriteTime[i].wYear, fc.m_vecFile
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值