How to get list of folders in this folder?
Asked
Active
Viewed 1.7k times
7
-
I regularly use the following link to reference the win 32 api as I myself am currently learning c++ http://msdn.microsoft.com/en-us/library/aa383749(VS.85).aspx but I would logically look for the word directories in the functions name but I cannot see anything. Nice question though! :-) – REA_ANDREW Feb 10 '10 at 19:56
-
The following seems to show a few ways to achieve what you want to do http://www.gamedev.net/community/forums/topic.asp?topic_id=177400&whichpage=1 – REA_ANDREW Feb 10 '10 at 19:58
-
2More dupes than you can shake a stick at, including http://stackoverflow.com/questions/306533/how-do-i-get-a-list-of-files-in-a-directory-in-c/306917 – Feb 10 '10 at 20:05
-
@Neil: No, not for this question and these tags. Witness that fact that the key word of the correct answer (FindExSearchLimitToDirectories) appears only once on SO. – MSalters Feb 11 '10 at 10:57
4 Answers
10
FindFirstFileEx+FindExSearchLimitToDirectories.
WIN32_FIND_DATA fi;
HANDLE h = FindFirstFileEx(
dir,
FindExInfoStandard,
&fi,
FindExSearchLimitToDirectories,
NULL,
0);
if (h != INVALID_HANDLE_VALUE) {
do {
if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
printf("%s\n", fi.cFileName);
} while (FindNextFile(h, &fi));
FindClose(h);
}
-
2FindExSearchLimitToDirectories is not really a reliable solution. It is an advisory flag only. For example on my Windows XP Pro SP3 system with NTFS it does not have any effect. See: http://stackoverflow.com/questions/2248911/file-system-support-for-findfirstfileex-limit-to-directories – Ash Feb 16 '10 at 04:32
-
I would expect that somebody would follow the links and read the documentation (which says "If the file system does not support directory filtering, this flag is silently ignored.") before using this code. Unreasonable expectation? – ephemient Feb 16 '10 at 06:32
-
2`I would expect that somebody would follow the links and read the documentation before using this code. Unreasonable expectation?` Yes, yes it is. If they wanted to read through docs and stuff, then why bother asking here? The point to asking directly is to get straight to the answer instead of spending all day searching and reading. – Synetech Jul 18 '15 at 17:45
8
If you can't use .NET & Managed code, you can go through the win32 api's
Here is an example that you can modify to only get Folders.
(Basically the following check:)
...
TCHAR szDir = _T("c:\\"); // or wherever.
HANDLE hFind = FindFirstFile(szDir, &ffd);
...
do {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// your code on 'ffd'
}
} while (FindNextFile(hFind, &ffd) != 0);

John Weldon
- 39,849
- 11
- 94
- 127
3
You can use Boost
Or, if you don't want Boost you can check out this thread where alternative options are discussed. http://www.gamedev.net/community/forums/topic.asp?topic_id=523375

Robert Greiner
- 29,049
- 9
- 65
- 85
1
For best portability, use the boost filesystem library. Use opendir()/readdir() and friends for UNIX based systems.

Vector Maniac
- 95
- 1
- 3