window C++ 遍历目录下所有文件

#include<io.h>
#include<iostream>
#include <fstream>  
#include <vector>  
#include <string> 
/*
* 判断文件中是否含有查找的字符串
*/
bool searchString(string filePath, string strF)
{
	std::ifstream file(filePath);
	if (!file.is_open()) {
		std::cerr << "无法打开文件: " << filePath << std::endl;
		return false;
	}

	std::string line;
	while (std::getline(file, line)) {
		if (line.find(strF) != std::string::npos) {
			file.close();
			return true; // 找到子字符串,返回true  
		}
	}
	file.close();
	return false; // 遍历完整个文件都没有找到,返回false  
}

/*	 
*	dir 子目录路径
*	vector<std::string>& folders 子目录
*   std::vector<std::string>& files 子文件
*	std::vector<std::string> filter= {} 过滤条件
*/

static void findStringFile(const std::string& dir, std::vector<std::string>& folders, std::vector<std::string>& files,std::vector<std::string> filter = {})
{
	struct _finddata_t file;
	std::string path = dir + "/*";
	auto h = _findfirst(path.c_str(), &file);
	if (h == -1) 
	{
		return;
	}

	do {
		if (0 == strncmp(file.name, ".", 1) || 0 == strncmp(file.name, "..", 2)) 
		{
			continue;
		}
		if (file.attrib == _A_SUBDIR) 
		{
			//	目录名字添加到folders
			folders.push_back(dir + "/" +file.name);
		}
		else
		{
			for (const auto &it : filter)
			{
				if (searchString(dir + "/" + file.name, it))
				{
					//	文件名字添加到folders
					files.push_back(dir + "/" + file.name);
				}
			}

		}
	} while (!_findnext(h,&file));

	_findclose(h);
}

/*	扫描目录下所有文件 非递归 水平优先
*	dir 目录路径
*/
static void searchdir(const std::string &dir, std::vector<std::string> &files, std::vector<std::string> filter = {})
{
	std::vector<std::string> folders;
	findStringFile(dir, folders, files, filter);

	while (folders.size())
	{
		std::vector<std::string> foldersChildren;
		for (const auto& foledr : folders)
		{
			std::vector<std::string> tfiles;
			std::vector<std::string> tfolders;
			findStringFile(foledr, tfolders, tfiles, filter);
			files.insert(files.end(), tfiles.begin(), tfiles.end());
			foldersChildren.insert(foldersChildren.end(), tfolders.begin(), tfolders.end());
		}

		folders = foldersChildren;
	}
}
/*	扫描目录下所有文件 非递归 垂直优先
*	dir 目录路径
*/
static void searchdirRecursion(const std::string &dir, std::vector<std::string> &files, std::vector<std::string> filter= {})
{
	std::vector<std::string> folders;
	struct _finddata_t file;
	std::string path = dir + "/*";
	auto h = _findfirst(path.c_str(), &file);
	if (h==-1)
	{
		return;
	}
	do{
		if (0==strncmp(file.name,".",1) || 0== strncmp(file.name,"..",2))
		{
			continue;
		}
		if (file.attrib==_A_SUBDIR)
		{
			path = dir + file.name;
			searchdirRecursion(path, files, filter);
		}
		else
		{
			for (const auto &it:filter)
			{
				if (searchString(dir + "/" + file.name, it))
				{
					//	文件名字添加到folders
					files.push_back(dir + "/" + file.name);
				}
			}

		}
	} while (!_findnext(h, &file));

	_findclose(h);
}

void main()
{
	//searchdir("E:/work/");
	std::vector<std::string> files;
	std::vector<std::string> filter;
	filter.push_back("hello");
	//searchdirRecursion("E:/log/", files, filter);
	searchdir("E:/log/", files, filter);
    return;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值