访问目录文件夹下的文件是经常需要的操作,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. 【开发者portal在线开发插件系列四】数组 及 可变长度数组

    基础篇 基础场景见上面两个帖子,这里单独说明数组和可变长度数组的用法. 话不多说,开始今天的演(表)示(演) Profile和插件开发 添加一个string类型的属性: 在插件里添加一条数据上报消息: ...

  2. shell 往文件中添加一列一样的字符串

    例如:往文件file.txt中,添加一列字符串"20161020", 用制表符分割 awk '$0=$0"\t20161020"' file.txt

  3. MySQL必知必会(创建计算字段(field))

    #字段(field)基本上和列(column)的意思相同 SELECT Concat(vend_name, ' (', vend_country, ')') FROM vendors ORDER BY ...

  4. go基础之服务退出问题

    最近学习公司微服务的代码,看到每一个微服务的main函数都阻塞在那里,然后里面起的goroutine一直在哪里运行. package main import( "fmt" &quo ...

  5. [TimLinux] python-ldap 介绍

    1. 接口 ldap: LDAP库接口 ldap.asyncsearch: 大量搜索结果数据采用流处理 ldap.controls: LDAPv3上层访问扩展控制 ldap.dn: LDAP dist ...

  6. 从零开始的openGL——四、纹理贴图与n次B样条曲线

    前言 在上篇文章中,介绍了如何加载绘制模型以及鼠标交互的实现,并且遗留了个问题,就是没有模型表面没有纹理,看起来很丑.这篇文章将介绍如何贴纹理,以及曲线的绘制. 纹理贴图 纹理加载 既然是贴图,那首先 ...

  7. Jmeter介绍以及脚本制作与调试

    目录 Jmeter介绍 Jmeter安装 Jmeter主要测试组件 Jmeter元件作用域与执行顺序 Jmeter运行原理 Jmeter脚本制作 Jmeter脚本调试 Jmeter介绍 Jmeter ...

  8. Ambari 自定义服务集成原理介绍

    之前,在 github 上开源了 ambari-Kylin 项目,可离线部署,支持 hdp 2.6+ 及 hdp 3.0+ .github 地址为:https://github.com/8418090 ...

  9. tabhost改变标签颜色

    package uiframe.zyx.uiframe.com.uiframe.fragments;import android.os.Bundle;import android.support.an ...

  10. 【ES6】数值的扩展

    1.Number.isFinite()和Number.isNaN()[只对数值有效] (1)Number.isFinite()用来检查一个数值是否为有限的(finite),即不是Infinity. [ ...