Click here to Skip to main content
16,017,297 members
Articles / Desktop Programming / MFC
Article

XFolderSize - A class to determine folder size

Rate me:
Please Sign up or sign in to vote.
4.83/5 (16 votes)
11 Jun 2007CPOL1 min read 61.8K   1.5K   42   12
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)


Written By
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

 
Questionproblem with new project integration Pin
danyseb29-Apr-16 8:00
danyseb29-Apr-16 8:00 
Questionminor fix Pin
Roger6528-Sep-12 12:13
Roger6528-Sep-12 12:13 
GeneralBig Memory Leak Pin
Mich2624-May-08 10:12
Mich2624-May-08 10:12 
GeneralRe: Big Memory Leak Pin
Mich2628-May-08 3:26
Mich2628-May-08 3:26 
I just found it. HANDLE hFF is never closed.

Use it like this:

*lpFolderSize = li_total;
if (lpFolderCount)
*lpFolderCount = dwTotalFolderCount;
if (lpFileCount)
*lpFileCount = dwTotalFileCount;

FindClose(hFF); // mem leak problem solved


Milan Ristic

AnswerRe: Big Memory Leak (handle leak actually) Pin
SlackerMonkey9-Sep-08 10:49
SlackerMonkey9-Sep-08 10:49 
GeneralRe: Big Memory Leak Pin
bbkingfan45120-Jan-09 12:26
bbkingfan45120-Jan-09 12:26 
AnswerRe: Big Memory Leak Pin
Hans Dietrich25-Apr-11 3:53
mentorHans Dietrich25-Apr-11 3:53 
Generaloccur error LNK2001 !!! Pin
kimtaehee26-Nov-07 15:31
kimtaehee26-Nov-07 15:31 
GeneralRe: occur error LNK2001 !!! Pin
Hans Dietrich27-Nov-07 5:49
mentorHans Dietrich27-Nov-07 5:49 
QuestionNetwork folders Pin
Evgeny Fedorischenko2-Oct-07 17:34
Evgeny Fedorischenko2-Oct-07 17:34 
Generalnice Pin
Anne_Hutchinson12-Jun-07 1:31
Anne_Hutchinson12-Jun-07 1:31 
AnswerRe: nice Pin
Hans Dietrich25-Apr-11 3:53
mentorHans Dietrich25-Apr-11 3: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.