Skip to content

Instantly share code, notes, and snippets.

@frenchie4111
Last active February 9, 2023 00:58
Show Gist options
  • Save frenchie4111/36316489cd266adc645e to your computer and use it in GitHub Desktop.
Save frenchie4111/36316489cd266adc645e to your computer and use it in GitHub Desktop.
Find All Files In Directory C++
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
void GetFilesInDirectory(std::vector<string> &out, const string &directory, bool getFolderNames)
{
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const string file_name = file_data.cFileName;
const string full_file_name = directory + "/" + file_name;
const bool is_directory = ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
if (file_name[0] == '.')
continue;
if (is_directory && !getFolderNames)
continue;
out.push_back(full_file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
}
int main() {
vector<string> files;
GetFilesInDirectory(files, "", true);
for( int i = 0; i < files.size(); i++ ) {
cout << files[i] << endl;
}
return 0;
}
@Ajax-Kroos
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment