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的转换。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

charlee44

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值