Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i Get Specified directory size included all files and folders in it with api function or sample code with recursively scan directory ?

Is there any way to get directory size without recursively scan ???

Thanks
Posted

I found these solutions via Google search:

A pure Win32 C++ example can be found here: How to find the size of all files, located inside a folder[^]

If you enable .NET Framework managed code in your C++ application, you can use this:
Scan the Directory / Files (Calculating the size)[^]. It does require recursion.

You can enable Managed C++ easily within your Native C++ project. If you are planning to use .NET System.IO.Directory, you can call in your native C++ project using the following setting.

Enable the "/clr" switch in Project Properties -> General -> Common Language Run time Support).

Now, your project will support .NET Framework classes. You can directly use managed C++ code in your programming.

Note:
I don't think there is any way to do it without a recursive scan. Think about how it would slow down your computer if every time you added something to a file, Windows had to update all the directory nodes above where your file was located.
 
Share this answer
 
v2
Comments
Majid_Abdollahi 22-Sep-13 9:32am    
i must implemented it in native c++ win32 without .Net then i like use sample code that using native c++.

Thanks.
Mike Meinz 22-Sep-13 9:38am    
I found a C++ Win32 API example and updated Solution 1 to include a link to that article.
Majid_Abdollahi 22-Sep-13 10:16am    
Thank Dear for refered sample of code but it just using String type but i must scan unicode names such as middle east languages then types of input path string must be Wstring or for example Wchar for scan unicode characters. :-(

Thanks.
Sergey Alexandrovich Kryukov 22-Sep-13 10:53am    
Not clear. Your original question was about the total size only. But what's the problem? Unicode names are supported.
—SA
Sergey Alexandrovich Kryukov 22-Sep-13 10:52am    
5ed.
—SA
If you a looking for a pure Win32 API solution without using .NET here is what you do:
C++
WIN32_FIND_DATAA data;
    HANDLE sh = NULL;

    sh = FindFirstFileA((_path+"\\*").c_str(), &data);

    if (sh == INVALID_HANDLE_VALUE )
    {
            return;
    }

    do
    {
        // skip current directory and parent directory
        if (std::string(data.cFileName).compare(".") != 0 &&  
            std::string(data.cFileName).compare("..") != 0)
        {

            // if found object is a ("...")
            if ((data.dwFileAttributes & 
                 FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY)
            {
                // we have reached a directory entry, 
               // then search inside of it recursively
                this->CalculateSize(_path + ''\\'' + data.cFileName);
            } 
            else
            {
                // get entry's size and add it to overall size
                this->dirSize += (__int64) (data.nFileSizeHigh *(MAXDWORD) +  
                                            data.nFileSizeLow);
            }
        }

    } while (FindNextFileA(sh, &data)); // do

    FindClose(sh);

Hope that helps.
 
Share this answer
 
v2
Comments
SandyM 1-Nov-13 6:04am    
Should be
... (MAXDWORD + 1)
See FindFirstFile[^] and its associated FindNextFile functions, to scan through a directory. You will need to make your code do the scans recursively if you wish to include all subdirectories.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900