遍历目标目录下的指定文件,未考虑子目录

本文介绍了一种使用C++从指定文件夹中获取所有特定扩展名文件的方法,并从中提取文件名。通过`GetTargetFileInFolder`函数实现,该函数接受文件夹路径和文件扩展名作为参数。
std::string getfilename(std::string sfilename)
{
	using namespace std;
	string sname = sfilename.substr(0, sfilename.find_first_of("."));
	return sname;
}
// get folder all file
std::vector<std::string> GetTargetFileInFolder(std::string sfolder, const std::string sfileext)
{
	using namespace std;
	sfolder += "\\" + sfileext;
	std::vector<string> vecFileName;
	WIN32_FIND_DATA FindFileData = {0};

	HANDLE hFile = FindFirstFile(sfolder.c_str(), &FindFileData);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		printf("hFile == INVALID_HANDLE_VALUE\n");
		return vecFileName;
	}
	else
	{
		vecFileName.push_back(getfilename(FindFileData.cFileName));
		while (FindNextFile(hFile, &FindFileData) != 0)
		{
			if (FindFileData.nFileSizeLow)
			{			
				vecFileName.push_back(getfilename(FindFileData.cFileName));
			}

		}
	}
	return vecFileName;

}

转载于:https://www.cnblogs.com/saiksy/archive/2011/06/20/2085208.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值