views:

193

answers:

5

How to get list of folders in this folder?

+3  A: 

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
+7  A: 

FindFirstFileEx+FindExSearchLimitToDirectories.

WIN32_FIND_DATA fi;
HANDLE h = FindFirstFileEx(
        dir,
        FindExInfoStandard,
        &fi,
        FindExSearchLimitToDirectories,
        NULL,
        0);
if (h != INVALID_HANDLE_VALUE) {
    do {
        printf("%s\n", fi.cFileName);
    } while (FindNextFile(h, &fi));
    FindClose(h);
}
ephemient
Nice... I didn't know about FindExSearchLimitToDirectories +1
John Weldon
FindExSearchLimitToDirectories 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
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
+1  A: 

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
+1  A: 

For best portability, use the boost filesystem library. Use opendir()/readdir() and friends for UNIX based systems.

Vector Maniac
`opendir()` etc. work just fine for me on Windows using MinGW.
Jon Purdy
A: 

I simply cannot get FindExSearchLimitToDirectories to work as expected, even on Windows 7. Has anyone actually seen this work as advertised?

Mark