How to find the size of a folder in C++

Here we see an approach using C++ to get the size of a folder. In this method, we use the string implementation and Windows API functions. The important two concepts are recursion and strings. They make our code reasonably simple and easy to understand and implement.

The size of a folder means the size of all of their smaller build-ups or sub-parts.

<windows.h> : It is a header file defined in C and C++ programming languages. It contains the declaration of all the windows APIs functions.

Also See:

to get the size of a folder using C++

We use TravelgivenDir () to travel the directory for all of its sub-directories and files. It sums all of their sizes and gives the output as their summation. Initializing the size to zero prevents garbage values from getting printed in some cases.

In this method, we use recursion to get the size of all the files in a folder. This allows us to check again and again until we are done checking sizes of all of the files. This is one of the most suitable methods for such cases since other methods tend to complicate it.


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

__int64 TravelgivenDir(string path_name)
    WIN32_FIND_DATA data_reqd;
    __int64 size = 0;
    string f_name = path_name + "\\*.*";//to find file name from corr. path name.
    HANDLE h1 = FindFirstFile(f_name.c_str(),&data_reqd);
    if(h1 != INVALID_HANDLE_VALUE)//to see if we encounter any unwanted or say invalid value.
        do {
            if( (data_reqd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
                // we skip "." and "..".  because we use strcmp here
                if( strcmp(data_reqd.cFileName,".") != 0 &&strcmp(data_reqd.cFileName,"..") != 0)
                    // sub-directory encountered:get the files in it also
                    f_name = path_name + "\\" + data_reqd.cFileName;
                    // recurrsionnnnnnnnn
                    size += TravelgivenDir(f_name);

                LARGE_INTEGER sz;
                // what we get here is the file size.
                // file sizes can be larger than 2 gigs therefore the size is written as two objects of DWORD type.
                // they combine to make 64 bit integer values.
                sz.LowPart = data_reqd.nFileSizeLow;
                sz.HighPart = data_reqd.nFileSizeHigh;
                size = size+ sz.QuadPart;

        }while( FindNextFile(h1,&data_reqd) != 0);//until we reach the end i.e. no more files are left

    return size;

//in main we take input as command line arguments
int main(int argc, char* argv[])
    __int64 size = 0;
    string path_name;
    size = TravelgivenDir("D:\\BMSCE");//entering the device file location
    cout << "\n\nThe Directory/Folder Size is = " << size << "\n";
    return 0;

Sample Output:

The Directory/Folder Size is = 6094231828

Also Read: program to get the size of a file in C++

Leave a Reply

Your email address will not be published. Required fields are marked *