Click here to Skip to main content
Click here to Skip to main content

XFolderSize - A class to determine folder size

, 11 Jun 2007 CPOL
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.

Introduction

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

    LARGE_INTEGER li;
    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:

screenshot

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

screenshot

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:
        LARGE_INTEGER li;
        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

Usage

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.



License

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

Share

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 www.hdsoft.org.






Comments and Discussions

 
Questionminor fix PinmemberRoger6528-Sep-12 13:13 
GeneralBig Memory Leak PinmemberMich2624-May-08 11:12 
If you press GetFolderSize 10 times, memory is increased by 1Mb. If you press it 100 times, it is increased to 10Mgb. So, it has memory leak... does anyone found a solution ?
 
Milan Ristic

GeneralRe: Big Memory Leak PinmemberMich2628-May-08 4:26 
AnswerRe: Big Memory Leak (handle leak actually) PinmemberSlackerMonkey9-Sep-08 11:49 
GeneralRe: Big Memory Leak Pinmemberbbkingfan45120-Jan-09 13:26 
AnswerRe: Big Memory Leak PinmentorHans Dietrich25-Apr-11 4:53 
Generaloccur error LNK2001 !!! Pinmemberkimtaehee26-Nov-07 16:31 
GeneralRe: occur error LNK2001 !!! PinmvpHans Dietrich27-Nov-07 6:49 
QuestionNetwork folders Pinmemberdafftin2-Oct-07 18:34 
Generalnice PinmemberAnne_Hutchinson12-Jun-07 2:31 
AnswerRe: nice PinmentorHans Dietrich25-Apr-11 4:53 

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

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

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