Skip to content

Instantly share code, notes, and snippets.

@Aurumaker72
Last active October 24, 2023 15:25
Show Gist options
  • Select an option

  • Save Aurumaker72/ae08a6c8dd2eac2a6ecfb1388ca75295 to your computer and use it in GitHub Desktop.

Select an option

Save Aurumaker72/ae08a6c8dd2eac2a6ecfb1388ca75295 to your computer and use it in GitHub Desktop.
Recursively find all files under directory
std::vector<std::string> get_files_in_subdirectories(
const std::string &directory)
{
std::string newdir = directory;
if (directory.back() != '\0')
{
newdir.push_back('\0');
}
WIN32_FIND_DATA find_file_data;
const HANDLE h_find = FindFirstFile((directory + "*").c_str(),
&find_file_data);
if (h_find == INVALID_HANDLE_VALUE)
{
return {};
}
std::vector<std::string> paths;
std::string fixed_path = directory;
do
{
if (strcmp(find_file_data.cFileName, ".") != 0 && strcmp(
find_file_data.cFileName, "..") != 0)
{
std::string full_path = directory + find_file_data.cFileName;
if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (directory[directory.size() - 2] == '\0') {
if (directory.back() == '\\') {
fixed_path.pop_back();
fixed_path.pop_back();
}
}
if (directory.back() != '\\') {
fixed_path.push_back('\\');
}
full_path = fixed_path + find_file_data.cFileName;
for (const auto& path : get_files_in_subdirectories(
full_path + "\\"))
{
paths.push_back(path);
}
} else
{
paths.push_back(full_path);
}
}
}
while (FindNextFile(h_find, &find_file_data) != 0);
FindClose(h_find);
return paths;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment