I'm trying to get all of the file with a certain pattern from a specific directory.
for example "ia_aiq_usecase_*.bin" from "c:\temp"
my windows code works fine.
however in linux I do not want to use boost. and I can't seem to find a way to get all the files with a certain pattern.
Can you please help ?
Is there a way to do it cross platform?
const std::vector<std::string> OS::GetAllFiles(std::string directoryPath, std::string filePattern)
{
#ifdef _WIN32
std::vector<std::string> files;
std::string pathAndPattern = directoryPath + OS::PathSeperator() + filePattern;
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(ToWString(pathAndPattern).c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
files.push_back(FromWString(fd.cFileName));
}
}
while (::FindNextFile(hFind, &fd));
{
::FindClose(hFind);
}
}
return files;
#else
std::vector<std::string> files;
std::string search_path = directoryPath + OS::PathSeperator() + filePattern;
DIR *dp;
struct dirent *dirp;
if ((dp = opendir(directoryPath.c_str())) == NULL)
{
std::cout << "Error(" << errno << ") opening " << directoryPath << std::endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
#endif
}