I work out a small project. I need to Get list of folders in each drive. Up to now I can get the drives but I have no Idea on how windows treats Drives. Does it treat them as super Directories (Super folders) that contain many sub-folders? If not How does it treat Them. And lastly how can I retrieve list of directories given drive name?

Thanks

Recommended Answers

All 6 Replies

MSDN says it treats like Top level directory
Then how do I get list of subdirectories?

Thanks

Check out AD's code snippet it's about file sizes but the code to traverse the directories should be along the lines of what you want. All else fails, google it again.

Suppose I want to iterate in the directory and print out only subdirectory, how do I do that? I cannot see folder handling there. am I missing something here?

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;


__int64 TransverseDirectory(string path)
{
    WIN32_FIND_DATA data;
    __int64 size = 0;
    string fname = path + "\\*.*";
    HANDLE h = FindFirstFile(fname.c_str(),&data);
    if(h != INVALID_HANDLE_VALUE)
    {
        do {
            if( (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
            {
                // make sure we skip "." and "..".  Have to use strcmp here because
                // some file names can start with a dot, so just testing for the
                // first dot is not suffient.
                if( strcmp(data.cFileName,".") != 0 &&strcmp(data.cFileName,"..") != 0)
                {
                    // We found a sub-directory, so get the files in it too
                    fname = path + "\\" + data.cFileName;
                    // recurrsion here!
                    size += TransverseDirectory(fname);
                }

            }
            else
            {
                LARGE_INTEGER sz;
                // All we want here is the file size.  Since file sizes can be larger
                // than 2 gig, the size is reported as two DWORD objects.  Below we
                // combine them to make one 64-bit integer.
                sz.LowPart = data.nFileSizeLow;
                sz.HighPart = data.nFileSizeHigh;
                size += sz.QuadPart;

            }
        }while( FindNextFile(h,&data) != 0);
        FindClose(h);

    }
    return size;
}

int main(int argc, char* argv[])
{
    __int64 size = 0;
    string path;
    size = TransverseDirectory("c:\\");
    cout << "\n\nDirectory Size = " << size << "\n";
    cin.ignore();
    return 0;
}

I imagine if you followed the if block of the do/while you could get all of the directories and just save the names instead of totaling the file sizes

I imagine if you followed the if block of the do/while you could get all of the directories and just save the names instead of totaling the file sizes

I'm at work and my head is saturated with today's wok. So I will have to go and do this at home after rest! ;)

I solved partly with wxWidgets. And will finish remaining part using C++ (MSDN have the remaining part well documented)

Thanks all for replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.