C++实现遍历指定文件或文件夹

来源:互联网 发布:网络视频存储方案 编辑:程序博客网 时间:2024/06/06 00:55

1. 概述

在这片文章中将讲诉用C++实现实现遍历指定文件和文件夹,一个是基于Win32平台开发的,一个是基于MFC平台开发的,在这里贴出来与大家分享。在完成过程中参考了这篇博客

2. 基于Win32平台开发

定义的两个成员变量
private:std::vector<std::string> filepath;//保存图像数据的位置信息char m_szInitDir[1000];//初始文件路径
由给定的文件夹进行遍历
//************************************************************************// 函数名称:    ReadFilenameFromFolder// 访问权限:    public // 创建日期:2016/12/26// 创 建 人:// 函数说明:读取一个目录下所有的Dcm文件名// 函数参数: std::string foldername 文件夹所在的文件夹路径// 返 回 值:   bool//************************************************************************bool CDcmRead::ReadFilenameFromFolder(std::string foldername){if (!this->GetFolderName(foldername))//判断路径可用性{std::cout << "输入文件路径错误无法获取" << std::endl;return false;}foldername = this->m_szInitDir;if (!this->BrowseFolder(foldername.c_str())){std::cout << "遍历文件夹失败" << std::endl;return false;}return true;}//************************************************************************// 函数名称:    BrowseFolder// 访问权限:    public // 创建日期:2016/12/26// 创 建 人:// 函数说明:遍历文件夹,提取出目录下和子目录的所有dcm文件名// 函数参数: const char * foler_name文件夹名称// 返 回 值:   bool//************************************************************************bool CDcmRead::BrowseFolder(const char* foler_name){_chdir(foler_name); //首先查找dir中符合要求的文件  long hFile;_finddata_t fileinfo;if ((hFile = _findfirst("*.dcm", &fileinfo)) != -1)//这里的 *.dcm 是我感兴趣的文件后缀名{do{//检查是不是目录  //如果不是,则进行处理  if (!(fileinfo.attrib & _A_SUBDIR)){char filename[_MAX_PATH];strcpy(filename, foler_name);strcat(filename, fileinfo.name);#ifdef DEBUGstd::cout << filename << std::endl;#endif // DEBUGthis->filepath.push_back(filename);}} while (_findnext(hFile, &fileinfo) == 0);_findclose(hFile);}//查找dir中的子目录  //因为在处理dir中的文件时,派生类的ProcessFile有可能改变了  //当前目录,因此还要重新设置当前目录为dir。  //执行过_findfirst后,可能系统记录下了相关信息,因此改变目录  //对_findnext没有影响。  _chdir(foler_name);if ((hFile = _findfirst("*.*", &fileinfo)) != -1){do{//检查是不是目录,如果是,再检查是不是 . 或 ..   //如果不是,进行迭代  if ((fileinfo.attrib & _A_SUBDIR)){if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0){char subdir[_MAX_PATH];strcpy(subdir, foler_name);strcat(subdir, fileinfo.name);strcat(subdir, "\\");#ifdef DEBUGstd::cout << "找到子文件夹: " << subdir << std::endl;#endif // DEBUGif (!this->BrowseFolder(subdir))return false;}}} while (_findnext(hFile, &fileinfo) == 0);_findclose(hFile);}return true;}//************************************************************************// 函数名称:    GetFolderName// 访问权限:    public // 创建日期:2016/12/26// 创 建 人:// 函数说明:规整化目录// 函数参数: std::string foldername传入的文件夹路径// 返 回 值:   bool//************************************************************************bool CDcmRead::GetFolderName(std::string foldername){//先把dir转换为绝对路径  if (_fullpath(m_szInitDir, foldername.c_str(), _MAX_PATH) == NULL){std::cout << "GetFolderName:获取输入路径的绝对路径失败!" << std::endl;return false;}//判断目录是否存在  if (_chdir(m_szInitDir) != 0){std::cout << "GetFolderName:输入的路径不存在!" << std::endl;return false;}//如果目录的最后一个字母不是'\',则在最后加上一个'\'  int len = strlen(m_szInitDir);if (m_szInitDir[len - 1] != '\\')strcat(m_szInitDir, "\\");return true;}
程序递归调用,将所有指定的文件后缀文件保存在filepath这个类成员变量中。

3. 基于MFC平台开发

//************************************************************************// 函数名称:    InitFileTree// 访问权限:    public // 创建日期:    2016/10/31// 创 建 人:    // 函数说明:    遍历目录下的所有文件夹和文件// 函数参数:    CString path需要遍历的文件夹// 函数参数:    HTREEITEM Parent节点的父结点// 返 回 值:    BOOL//************************************************************************BOOL CMyMenueTab1::InitFileTree(CString path, HTREEITEM Parent){if (path == _T("")){return FALSE;}else{if (path.Right(1) != _T("\\"))path += L"\\";path = path + _T("*.*");}CFileFind finder;CString strPath;BOOL bWorking = finder.FindFile(path);while (bWorking){bWorking = finder.FindNextFile();strPath = finder.GetFilePath();if (finder.IsDirectory() && !finder.IsDots()){CString m_FolderName = strPath.Right(strPath.GetLength() - path.GetLength() + 3);HTREEITEM m_subParent = this->m_FileTree.InsertItem(m_FolderName, 0, 0, Parent, TVI_LAST);this->InitFileTree(strPath, m_subParent); //递归调用}//文件夹else if (!finder.IsDirectory() && !finder.IsDots()){CString m_FolderName = strPath.Right(strPath.GetLength() - path.GetLength() + 3);this->m_FileTree.InsertItem(m_FolderName, 1, 1, Parent, TVI_LAST);}//文件}return TRUE;}
上面的这段代码相对于Win32的要简洁了很多,传入一个路径之后就开始读取指定文件夹下的所有文件和文件夹。运行的到的结果:


0 0
原创粉丝点击