Click here to Skip to main content
13,298,655 members (48,760 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 11 Jun 2007

XFolderSize - A class to determine folder size

, 11 Jun 2007
Rate this:
Please Sign up or sign in to vote.
XFolderSize is a non-MFC class that collects folder size, folder count, and file count based on starting folder.


XFolderSize is small class to get folder size information based on starting folder. You use XFolderSize like this:

DWORD dwFolderCount = 0;
DWORD dwFileCount = 0;

CXFolderSize fs;

BOOL rc = fs.GetFolderSize(_T("C:\\myfolder"), TRUE, TRUE, &li,
                    &dwFolderCount, &dwFileCount);

The GetFolderSize() function allows you to specify whether to recurse into subdirectories, whether to treat root directory of drive in special way (using SHGetDiskFreeSpace() to get total size), and optionally will return folder count and file count.

Because file sizes may be over 4 Gb, the LARGE_INTEGER type is used. This is what is returned by Win32 API GetFileSizeEx():

BOOL GetFileSizeEx(HANDLE hFile,               // handle to file
                   PLARGE_INTEGER lpFileSize); // file size

XFolderSize may be used only in Windows 2000 and later, since GetFileSizeEx() is not available in earlier versions of Windows.

Implementation Details

XFolderSize offers two functions:
  • GetFolderSize():
    // GetFolderSize()
    // Purpose:     Get folder size
    // Parameters:  lpszStartFolder - fully qualified starting folder path
    //              bRecurse        - TRUE = recurse into subdirectories
    //              bQuickSize      - TRUE = if lpszStartFolder is a drive, use 
    //                                SHGetDiskFreeSpace() to get size for entire
    //                                disk, instead of recursing (folder count
    //                                and file count will be 0).
    //              lpFolderSize    - pointer to 64-bit folder size
    //              lpFolderCount   - pointer to 32-bit folder count (optional,
    //                                may be NULL)
    //              lpFileCount     - pointer to 64-bit file count (optional,
    //                                may be NULL)
    // Returns:     BOOL - Returns nonzero if successful or zero otherwise.
    BOOL CXFolderSize::GetFolderSize(LPCTSTR lpszStartFolder, 
                       BOOL bRecurse, 
                       BOOL bQuickSize,
                       PLARGE_INTEGER lpFolderSize,
                       LPDWORD lpFolderCount /*= NULL*/,
                       LPDWORD lpFileCount /*= NULL*/)
  • GetFileSize64():
    // GetFileSize64()
    // Purpose:     Get 64-bit file size
    // Parameters:  lpszPath   - fully qualified path to file
    //              lpFileSize - 64-bit file size
    // Returns:     BOOL - Returns nonzero if successful or zero otherwise.
    BOOL CXFolderSize::GetFileSize64(LPCTSTR lpszPath, PLARGE_INTEGER lpFileSize)

Demo App

The demo app allows you to try out XFolderSize on your own system:


You can also use Quick Size option to get the size used by an entire drive:


How To Use

To integrate CXFolderSize class into your app, do the following:

  1. You first need to add following files to your project:
    • XFolderSize.cpp
    • XFolderSize.h
  2. In Visual Studio settings, select Not using pre-compiled header for XFolderSize.cpp.
  3. Next, include header file XFolderSize.h in source file where you want to use CXFolderSize.
  4. Now you are ready to start using CXFolderSize. Your code should look something like:
    DWORD dwFolderCount = 0;
    DWORD dwFileCount = 0;
    CXFolderSize fs;
    BOOL rc = fs.GetFolderSize(_T("C:\\myfolder"), TRUE, TRUE, &li,
                        &dwFolderCount, &dwFileCount);

Revision History

Version 1.0 — 2007 June 11

  • Initial public release


This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


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


About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see

You may also be interested in...


Comments and Discussions

Questionproblem with new project integration Pin
danyseb29-Apr-16 9:00
memberdanyseb29-Apr-16 9:00 
Questionminor fix Pin
Roger6528-Sep-12 13:13
memberRoger6528-Sep-12 13:13 
GeneralBig Memory Leak Pin
Mich2624-May-08 11:12
memberMich2624-May-08 11:12 
GeneralRe: Big Memory Leak Pin
Mich2628-May-08 4:26
memberMich2628-May-08 4:26 
AnswerRe: Big Memory Leak (handle leak actually) Pin
SlackerMonkey9-Sep-08 11:49
memberSlackerMonkey9-Sep-08 11:49 
I ran into this problem--and the ended up at the same solution--but just to clarify... the original code will leak a handle on the specified folder, so in addition to the memory leaked, you will not be able to modified (e.g. delete, rename, move) the directory until after the process terminates.
GeneralRe: Big Memory Leak Pin
bbkingfan45120-Jan-09 13:26
memberbbkingfan45120-Jan-09 13:26 
AnswerRe: Big Memory Leak Pin
Hans Dietrich25-Apr-11 4:53
mentorHans Dietrich25-Apr-11 4:53 
Generaloccur error LNK2001 !!! Pin
kimtaehee26-Nov-07 16:31
memberkimtaehee26-Nov-07 16:31 
GeneralRe: occur error LNK2001 !!! Pin
Hans Dietrich27-Nov-07 6:49
mvpHans Dietrich27-Nov-07 6:49 
QuestionNetwork folders Pin
dafftin2-Oct-07 18:34
memberdafftin2-Oct-07 18:34 
Generalnice Pin
Anne_Hutchinson12-Jun-07 2:31
memberAnne_Hutchinson12-Jun-07 2:31 
AnswerRe: nice Pin
Hans Dietrich25-Apr-11 4:53
mentorHans Dietrich25-Apr-11 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 11 Jun 2007
Article Copyright 2007 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid