访问目录文件夹下的文件是经常需要的操作,C/C++和win32接口都没有提供直接调用的函数。在这里总结了几个经常用到的函数,通过MFC的CFileFind函数递归遍历实现,包括以下几个功能函数:

  1. 查找目录下所有的文件夹;
  2. 查找目录下所有的文件(不遍历目录的目录);
  3. 查找目录下所有的文件(遍历目录的目录) ;
  4. 查找目录下某一类型文件 (不遍历目录的目录);
  5. 查找目录下某一类型文件 (遍历目录的目录);
//查找目录下所有的文件夹
void FindFolder(string dir, vector<string> &folderPath)
{
CFileFind fileFinder;
CString filePath = CString(dir.c_str()) + _T("\\*.*"); BOOL bFinished = fileFinder.FindFile(filePath);
while (bFinished) //每次循环对应一个类别目录
{
bFinished = fileFinder.FindNextFile();
if (fileFinder.IsDirectory() && !fileFinder.IsDots()) //fileFinder.IsDots()识别"."文件和".."文件
{
CString filePath = fileFinder.GetFilePath();
folderPath.push_back(filePath.GetBuffer());
filePath.ReleaseBuffer();
}
} fileFinder.Close();
} //查找目录下所有的文件(不遍历目录的目录)
void FindDirFileNoFormat(string dir, vector<string> &filePath)
{
CFileFind fileFinder;
CString path = CString(dir.c_str()) + _T("\\*.*"); BOOL bFinished = fileFinder.FindFile(path);
while (bFinished) //每次循环对应一个类别目录
{
bFinished = fileFinder.FindNextFile();
if (fileFinder.IsDirectory() || fileFinder.IsDots()) //fileFinder.IsDots()识别"."文件和".."文件
{
continue;
}
else
{
CString findPath = fileFinder.GetFilePath();
filePath.push_back(findPath.GetBuffer());
findPath.ReleaseBuffer();
}
} fileFinder.Close();
} //查找目录下所有的文件(遍历目录的目录)
void FindAllFileNoFormat(string dir, vector<string> &filePath)
{
CFileFind fileFinder;
CString path = CString(dir.c_str()) + _T("\\*.*"); BOOL bFinished = fileFinder.FindFile(path);
while (bFinished) //每次循环对应一个类别目录
{
bFinished = fileFinder.FindNextFile(); // 跳过 . 和 .. ; 否则会陷入无限循环中!!!
if (fileFinder.IsDots())
{
continue;
} //
if (fileFinder.IsDirectory())
{
CString findPath = fileFinder.GetFilePath();
string subdir = findPath.GetBuffer();
FindAllFileNoFormat(subdir, filePath);
findPath.ReleaseBuffer();
}
else
{
CString findPath = fileFinder.GetFilePath();
filePath.push_back(findPath.GetBuffer());
findPath.ReleaseBuffer();
}
} fileFinder.Close();
} // 查找目录下某一类型文件 (不遍历目录的目录)
void FindDirFile(string dir, string format, vector<string> &filePath)
{
CFileFind fileFinder;
CString path = CString(dir.c_str()) + _T("\\*") + CString(format.c_str()); BOOL bFinished = fileFinder.FindFile(path);
while (bFinished) //每次循环对应一个类别目录
{
bFinished = fileFinder.FindNextFile();
if (fileFinder.IsDirectory() && !fileFinder.IsDots()) //fileFinder.IsDots()识别"."文件和".."文件
{
continue;
}
else
{
CString findPath = fileFinder.GetFilePath();
filePath.push_back(findPath.GetBuffer());
findPath.ReleaseBuffer();
}
} fileFinder.Close();
} //得到文件路径的格式后缀
string GetPathFormat(string filePath)
{
string format = filePath;
size_t p = filePath.find_last_of('.');
if (p == -1)
{
return string();
}
format.erase(0, p);
return format;
} // 查找目录下某一类型文件 (遍历目录的目录)
void FindDirAllFileEx(string dir, vector<string> &format, vector<string>& filePath)
{
CFileFind fileFinder;
CString path = CString(dir.c_str()) + _T("\\*.*"); BOOL bFinished = fileFinder.FindFile(path);
while (bFinished) //每次循环对应一个类别目录
{
bFinished = fileFinder.FindNextFile(); // 跳过 . 和 .. ; 否则会陷入无限循环中!!!
if (fileFinder.IsDots())
{
continue;
} if (fileFinder.IsDirectory())
{
CString findPath = fileFinder.GetFilePath();
string subdir = findPath.GetBuffer();
FindDirAllFileEx(subdir, format, filePath);
findPath.ReleaseBuffer();
}
else
{
//获取文件类型
CString findPath = fileFinder.GetFilePath();
string file = findPath.GetBuffer();
string postfix = GetPathFormat(file); bool flag = false;
for (auto it : format)
{
if (_stricmp(it.c_str(), postfix.c_str()) == 0)
{
flag = true;
break;
}
} if (flag)
{
filePath.push_back(file);
} findPath.ReleaseBuffer();
}
} fileFinder.Close();
}

有一点值得注意的是我这里函数的参数都封装成STL的string,在多字节下可以直接使用,在unicode下需要稍微修改下CString与string的转换。

VC遍历访问目录下的文件的更多相关文章

  1. C#.NET中遍历指定目录下的文件(及所有子目录及子目录里更深层目录里的文件)

    //遍历一个目录下所有的文件列表,代码实例 DirectoryInfo dir = new DirectoryInfo(folderName);var list = GetAll(dir); /// ...

  2. Windows下遍历某目录下的文件

    需求:要求遍历某个目录下的所有文件,文件夹 之前遇到过一些参考程序,其中有一种方法只能遍历 FAT32 格式的目录, 无法遍历NTFS的目录.

  3. java-IO流(File对象-深度遍历指定目录下的文件夹和文件)

    需求:遍历这个树状结构 File(String pathname) '\\'为了转义'\' // 通过抽象路径pathname 创建一个新的文件或者目录 File parent = new File( ...

  4. OpenCV代码提取:遍历指定目录下指定文件的实现

    前言 OpenCV 3.1之前的版本,在contrib目录下有提供遍历文件的函数,用起来比较方便.但是在最新的OpenCV 3.1版本给去除掉了.为了以后使用方便,这里将OpenCV 2.4.9中相关 ...

  5. node遍历给定目录下特定文件,内容合并到一个文件

    遍历目录用了fs.readdir这个异步方法,得到当前目录下所有的文件和目录的一个数组.然后判断: if文件,并且后缀符合设定的规则(本文例子是符合后缀ts,js)直接用同步方法写入, if目录,继续 ...

  6. Lua 遍历Linux目录下的文件夹

    代码如下,里面有注释,应该能看懂. function getFile(file_name) local f = assert(io.open(file_name, 'r')) local string ...

  7. windows代码,传入文件名,遍历此目录下所有文件.

    #include <windows.h> #include <vector> using namespace std; BOOL IterAtorFileSaveFile(IN ...

  8. java 遍历指定目录下的文件夹并查找包含指定关键字的文件

    输入指定关键字,在制定目录中查找包含关键字的文件,返回包含指定关键字的文件路径. package net.xsoftlab.baike; import java.io.File; import jav ...

  9. java递归遍历获取目录下所有文件

    import java.io.File; import java.util.ArrayList; import java.util.List; public class GetFiles { Arra ...

随机推荐

  1. iOS Core Animation 简明系列教程

    iOS Core Animation 简明系列教程  看到无数的CA教程,都非常的难懂,各种事务各种图层关系看的人头大.自己就想用通俗的语言翻译给大家听,尽可能准确表达,如果哪里有问题,请您指出我会尽 ...

  2. mybatis 和servlet使用MVC实现用户登录,注册,退出

    普通实现: USerMapper.java: package com.bjsxt.mapper; import org.apache.ibatis.annotations.Param; import ...

  3. GIS学习汇总

    GIS之家: Geoserver: geoserver安装部署步骤 geoserver发布地图服务WMS geoserver发布地图服务WMTS geoserver集成以及部署arcgis serve ...

  4. HDU5973 Game of Geting Stone(威佐夫博弈)

    Two people face two piles of stones and make a game. They take turns to take stones. As game rules, ...

  5. Mysql基础02-约束

    约束与索引 概念 1.数据完整性(Data Integrity)是指数据的精确性(Accuracy)和可靠性(Reliability). 实体完整性(Entity Integrity):例如,同一个表 ...

  6. 笔记||Python3之字典

    字典的定义与特性: 字典的每个键值key ==> value 对用冒号:分割,每个键值对之间用逗号分割,整个字典包括在花括号{}中. 字典名 = {键名1:值1, 键名2:值2} 如:dict ...

  7. DENEBOLA (See3CAM_CX3RDK) - CX3 Reference Design

    Denebola (See3CAM_CX3RDK) is a USB3.0 USB video class (UVC) reference design kit (RDK) developed by ...

  8. 【Web技术】401- 在 React 中使用 Shadow DOM

    本文作者:houfeng 1. Shadow DOM 是什么 Shadow DOM 是什么?我们先来打开 Chrome 的 DevTool,并在 'Settings -> Preferences ...

  9. JS数据结构——队列

    创建一个自己的类来表示一个队列 function Queue() { //这里写属性和方法 } 首先需要一个用于存储队列中元素的数据结构,可以用数组 let items = [] 接下来声明一些队列可 ...

  10. ES6对数组的扩展(简要总结)

    文章目录 数组的扩展(ES6) 1. 扩展运算符 2. Array.from 3. Array.of() 4. copyWithin() 5. find() 和 findIndex() 6. fill ...