XFolderSize - A class to determine folder size






4.83/5 (15 votes)
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:
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:
-
You first need to add following files to your project:
- XFolderSize.cpp
- XFolderSize.h
- In Visual Studio settings, select Not using pre-compiled header for XFolderSize.cpp.
- Next, include header file
XFolderSize.h in source file where you want to use
CXFolderSize
. - 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.