Recursive File Searching using Windows API:

There are different libraries you could use in Windows to list files or in general play with them, C++ Boost  is a good example.

However, in this post I’m going to show you how it can be done using Windows Native APIs. All you need to do is to call “FindFirstFile()” and then iterate thought it’s elements with “FindNextFile()”. The code itself is pretty much self explanatory and it will look for a given file extension in given folder and it’s all sub-directories.

Here is the code in C++:

The parts i have commented are used for debugging purposes.
Useful resource if you want it to work with Unicode files as well:
Compile in ANSII mode in Visual Studio won't work with Unicode characters :)
#include <Windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <tchar.h>
using namespace std;
vector<string> FilesPaths(const string &path, const string &filter);
static int filenum = 0;
int main(int argc, char **argv)
//Supply the parameters , no error checking cuz i am too lazy :D
	vector m_Files = FilesPaths(argv[1], argv[2]);
	//FilesPaths("C:\\Users\\Saeed\\Pictures", "txt");
	for (auto &str : m_Files)
		cout << "[+]" << str << endl;
	cout << "Number of Total files: " << filenum << endl;
vector<string> FilesPaths(const string &rootPath, const string &filter)
	vector<string> ListOfFiles;
	WIN32_FIND_DATA file;
	string search_str = rootPath +"\\*";
	//cout << "[Target dir: " << search_str << "] " << endl;
	hSearch = FindFirstFile(search_str.c_str(), &file);
		cerr << "[!]Faild to get search handle" << endl;
		return ListOfFiles;
	if ((strcmp(file.cFileName, ".") == 0) || (strcmp(file.cFileName, "..") ==0))
	if (file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    //cout << "<<<<<<<<<<<<<<<<DIR  >>>>>\t" << file.cFileName << endl;
		vector<string> temp = FilesPaths(rootPath + "\\" + file.cFileName , filter);
		for (auto &str : temp)
	else {
	size_t position = std::string(file.cFileName).find_last_of(".");
	string temp = std::string(file.cFileName).substr(position + 1);
		if (temp.find(filter) != string::npos)
		ListOfFiles.push_back(rootPath + "\\" + file.cFileName);
	} while (FindNextFile(hSearch, &file));
	return ListOfFiles;

Once complied you need to provide arguments from command-line as in example below:
This is an example to look for “exe” files in directory.

C:\SearchFiles.exe "C:\Users\SaEeD\Desktop" exe
Number of Total files: 7

I hope you find this useful. Thanks and good luck :)