I need to find a fews file based on a pattern: C:\Users\Admin\Desktop\*\cities.json
and countries.json
. Basically it is on the Desktop, but it can be in any folder there.
I found a similar function posted by Thomas Bonini. I don't really need the linux part. How can I do that?
/* Returns a list of files in a directory (except the ones that begin with a dot) */
void GetFilesInDirectory(std::vector<string> &out, const string &directory)
{
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)
continue;
out.push_back(full_file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
} // GetFilesInDirectory
Edit:
Based on @Someprogrammerdude's comment. It works well, but both of these files should be in a same folder. For ex. If cities.json
is presented but countries.json
is not in the directory, then it should be ignored. How do I perform that check as well?
std::vector<std::filesystem::path> test()
{
auto vec{ std::vector<std::filesystem::path> {} };
for (const auto& entry : std::filesystem::recursive_directory_iterator(L"C:\\Users\\Admin\\Desktop\\"))
{
if (entry.is_regular_file())
{
const auto& full_path = entry.path();
if (const auto filename = entry.path().filename(); filename == L"countries.json" || filename == L"cities.json")
{
vec.push_back(full_path);
}
}
}
return vec;
}