Reputation: 141
How to get all files in a given directory using C++ on windows?
Note:
I found methods that use dirent.h
but I need a more standard way...
Thanks
Upvotes: 14
Views: 30317
Reputation: 179907
The accepted standard for C++ is described in N1975 ISO/IEC TS 18822:2015, latest draft is N4100. Your compiler might not have it yet, in which case Boost.FileSystem provides essentially the same.
Upvotes: 4
Reputation: 6933
You have to use the FindFirstFile
function (documented here). This is the standard (and preferred) way in Windows, however it is not portable. The header dirent.h
you have found contains the definition of the standard POSIX functions.
For the full code look at this example: Listing the Files in a Directory
Upvotes: 5
Reputation: 70701
Use FindFirstFile and related functions. Example:
HANDLE hFind;
WIN32_FIND_DATA data;
hFind = FindFirstFile("c:\\*.*", &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
printf("%s\n", data.cFileName);
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
Upvotes: 30