VC遍历访问目录下的文件

访问目录文件夹下的文件是经常需要的操作,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的转换。

posted @   charlee44  阅读(1076)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示