遍历文件夹下指定的文件类型

xiaoxiao2021-02-28  101

#include <vector> #include <afxtempl.h> #include <Shlwapi.h> #define _VEC_FILE_ std::vector<std::pair<CString, CString>>  //保存文件路径和名称容器 /*  *  函数:  * GetSendFilePath(获取需要发送的文件)  *  参数:  * vecFilePath:装载需要发送的文件路径容器  * strFileType:遍历文件类型(*.xml ,*.*,*.jpg等类型)  *  返回值:  * 返回需要发送文件的总数  * ssdwujianhua 2017/06/06   */ size_t CThreadSanjifenfaReport::GetSendFilePath(_VEC_FILE_ &vecFilePath, CSting strFileType) { //遍历当前文件夹中的所有文件 CString strPath = m_sjffInfo.strSaveReportFilePath; if ( !strPath.IsEmpty() ) { CFileFind fileFinder;//类CFileFind执行本地文件查找   CString strPicFile; CString strFilePath;//保存文件路径   CString strFileName;//保存文件名   do   {   if (strPath.Right(1) == TEXT("\\"))   {   int nPos  = strPath.ReverseFind(TEXT('\\'));   strPath = strPath.Left(nPos);   }   strPicFile.Format(TEXT("%s\\%s"),strPath,strFileType); BOOL bWorking = fileFinder.FindFile(strPicFile);   if ( !bWorking ) { break; } while (bWorking)   {      bWorking = fileFinder.FindNextFile();   if (fileFinder.IsDots())//IsDots判断是否为点,由CFileFind对象引用IsDots的意思是:这是一个目录并且不是这个目录本身或者上层目录   {   continue;   }   strFilePath = fileFinder.GetFilePath();//文件的完整路径   strFileName = fileFinder.GetFileName();//文件的名字   if (fileFinder.IsDirectory())//检查是否是文件夹,是返回true,不是返回false   {      //继续遍历目录   continue;   }   else   {      int nPos = strFilePath.ReverseFind(TEXT('.'));   CString strExt = strFilePath.Right(strFilePath.GetLength() - nPos - 1);   if (strExt.CompareNoCase(strFileType)  == 0)   {      vecFilePath.push_back(std::pair<CString, CString>(strFilePath, strFileName)); }   }   }   }while(fileFinder.FindNextFile());   fileFinder.Close(); } return vecFilePath.size(); }

转载请注明原文地址: https://www.6miu.com/read-43504.html

最新回复(0)