fix: make get_files_from_dir works with absolute path (#598)

Co-authored-by: Matti Pulkkinen <pulkkinen@ultimatium.com>
This commit is contained in:
Matti Pulkkinen 2025-02-22 15:16:50 +02:00 committed by GitHub
parent 59ca2b0f16
commit 3753223982
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -113,19 +113,32 @@ std::vector<std::string> get_files_from_dir(const std::string& dir) {
// Find the first file in the directory // Find the first file in the directory
hFind = FindFirstFile(directoryPath, &findFileData); hFind = FindFirstFile(directoryPath, &findFileData);
bool isAbsolutePath = false;
// Check if the directory was found // Check if the directory was found
if (hFind == INVALID_HANDLE_VALUE) { if (hFind == INVALID_HANDLE_VALUE) {
printf("Unable to find directory.\n"); printf("Unable to find directory. Try with original path \n");
char directoryPathAbsolute[MAX_PATH];
sprintf(directoryPathAbsolute, "%s*", dir.c_str());
hFind = FindFirstFile(directoryPathAbsolute, &findFileData);
isAbsolutePath = true;
if (hFind == INVALID_HANDLE_VALUE) {
printf("Absolute path was also wrong.\n");
return files; return files;
} }
}
// Loop through all files in the directory // Loop through all files in the directory
do { do {
// Check if the found file is a regular file (not a directory) // Check if the found file is a regular file (not a directory)
if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
if (isAbsolutePath) {
files.push_back(dir + "\\" + std::string(findFileData.cFileName));
} else {
files.push_back(std::string(currentDirectory) + "\\" + dir + "\\" + std::string(findFileData.cFileName)); files.push_back(std::string(currentDirectory) + "\\" + dir + "\\" + std::string(findFileData.cFileName));
} }
}
} while (FindNextFile(hFind, &findFileData) != 0); } while (FindNextFile(hFind, &findFileData) != 0);
// Close the handle // Close the handle