遍历文件夹下某种类型文件名(C++)

时间:2021-02-18 06:31:39

在文件夹下,查找某后缀的文件:

  
vector<string> findfileinfolder(string fileFolderPath, string fileExtension)//文件路径和后缀名
{
	string fileFolder = fileFolderPath + "\\*." + fileExtension;
	vector<string> file;

	char fileName[2000];

	struct _finddata_t fileInfo;    

	long findResult = _findfirst(fileFolder.c_str(), &fileInfo);
	if (findResult == -1)
	{
		_findclose(findResult);
		return file;
	}
	bool flag = 0;

	do
	{
		sprintf(fileName, "%s\\%s", fileFolderPath.c_str(), fileInfo.name);

		if (fileInfo.attrib == _A_ARCH)  
		{
			file.push_back(fileName);
			//Mat frame = imread(fileName, 1);
			//imshow("1", frame);
		}

	} while (!_findnext(findResult, &fileInfo));

	_findclose(findResult);


	return file;
}


……
vector<string> jpg = findfileinfolder("D:\\图片","jpg");
……