• 11
• 27
• 9
• 20
• 31

# [Win32 C++] List all sub-dirs in root directory

This topic is 2680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I am using just win32 functions to list all sub directories in a root directory. I can do it & it works just fine, but it seems not very efficient because it involves opening the directory(handle to it) & iterating over every single file in the directory, checking if that file is a directory or a file then moving onto the next one.

So in short, in order to identify all the sub-dirs in a root directory I need to check EVERY file. I have found that each directory contains a directory with the name ".." & it lists all the directories contained in the upper/parent directory of the root directory, maybe that is a way to get a list of the sub-dirs? But is the ".." dir a feature of all windows versions?

Is there a more efficient way to obtain a list of all the sub directories within a root directory?

stack <string> getSubDirectories( string rootDir, unsigned int degree ){    // Post: Search a directory & store the absolute path of any sub-    //       directories (with X degree of separation) identified within    //       the root directory.            rootDir = formatFilePath( rootDir );        WIN32_FIND_DATA  dirData;    HANDLE           dir = openDirectory( rootDir, dirData );  // getDirectoryListing( rootDir, dirData );    stack <string>   subDirs;        if ( dir == NULL )    {         printf( "No directory log found inside the root directory %s \n", rootDir.c_str() );         return stack <string>();    }            while ( FindNextFile( dir, &dirData ) != 0 )    {         if ( string(dirData.cFileName) != ".."  &&  isDirectory( dirData.dwFileAttributes ) )         {              string subDirectory = rootDir.substr(0, rootDir.length() - 1) + string( dirData.cFileName );              subDirs.push( subDirectory );                            if ( degree > 1 )              {                 subDirs += getSubDirectories( subDirectory, degree - 1 );              }         }    }        FindClose( dir );            return subDirs;    }