Last active
February 9, 2023 00:58
-
-
Save frenchie4111/36316489cd266adc645e to your computer and use it in GitHub Desktop.
Find All Files In Directory C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!