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

Started by
0 comments, last by gretty 13 years, 5 months ago
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;    }
Advertisement
Well I got it working by accessing the ".." of the n+1 sub-dir so thats cool :)

But I am still not sure if the ".." directory is something that occurs in most(is it all?) directories is an aspect of all versions of windows?

Also what would be the correct name for the ".." directory, is it the "Directory log directory"? Or "Parent Directory Listing"?

This topic is closed to new replies.

Advertisement