windows 遍历 特定目录下文件

参考自 :https://blog.csdn.net/timmiy/article/details/51695323

很多类似代码都有 wchar char 等类型问题 + _s 问题。

一下代码在 windows10  VS2017上亲测可用

// CMD-Test.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#include 
#include 

using namespace std;

void FindFile(string strPath, vector * vec);
static int total = 0;
int main()
{
	//int i;
	//i = system("D:\\测试软件\\UniExtract\\UniExtract.exe d:\\测试软件\\windows.iso d:\\测试软件\\test2");
	//printf("i = %d\n", i);
	vector vec;

	string strFilePath = "e:\\extractFiles";
	FindFile(strFilePath,&vec);

	printf("total = %d\n", total);
	getchar();
	return 0;
}


void FindFile(string strPath, vector * vec)
{

	WIN32_FIND_DATAA  findData = { 0 };
	string strFindPath = strPath + "\\*.*";
	//查找第一个文件  
	HANDLE hFindFine = FindFirstFileA(strFindPath.c_str(), &findData);
	if (INVALID_HANDLE_VALUE == hFindFine)
		printf("Error:%d", GetLastError());
	//循环递归查找文件  
	do
	{
		//判断该文件是否为文件夹  
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (findData.cFileName[0] == '.')
				continue;
			cout << "在" << strPath << "找到文件夹:\t" << findData.cFileName << endl;
			string strNeedFindPaht = strPath + "\\" + findData.cFileName;
			//若该文件是文件夹,则递归进行查找该文件夹下的文件  
			FindFile(strNeedFindPaht,  vec);
		}
		else
		{
			cout << "在" << strPath << "找到文件:\t" << findData.cFileName << endl;
			vec->push_back(strPath + "\\" + findData.cFileName);
			
		}
	} while (FindNextFileA(hFindFine, &findData));
	//关闭文件搜索句柄  
	FindClose(hFindFine);
}




你可能感兴趣的:(C++)