How can I determine the list of files in a directory from inside my C or C++ code?
I\'m not allowed to execute the ls
command and parse the results from
One function is enough, you don't need to use any 3rd-party library (for Windows).
#include
vector get_all_files_names_within_folder(string folder)
{
vector names;
string search_path = folder + "/*.*";
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
if(hFind != INVALID_HANDLE_VALUE) {
do {
// read all (real) files in current folder
// , delete '!' read other 2 default folder . and ..
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
names.push_back(fd.cFileName);
}
}while(::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
return names;
}
PS: as mentioned by @Sebastian, you could change *.*
to *.ext
in order to get only the EXT-files (i.e. of a specific type) in that directory.