Click here to Skip to main content
13,090,267 members (90,458 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


27 bookmarked
Posted 10 Dec 2009

DiskManger - GetFolderSize and CleanupFolder

, 10 Dec 2009
Rate this:
Please Sign up or sign in to vote.
A class to check disk information like total size, free space, used space, and clean up.


The DiskManager is a small class written in native C++. It is a simple class to check disk information like total size, free space, used space, and clean up.

I faced a problem while my a growing directory eventually took up my huge disk space. I used to dynamically create a subfolder in a few directories and store large files into them. An idea to solve the growing folder size was to introduce a self-managed mechanism to prevent a directory from growing beyond a specified size (user defined), like 10 GB or 20 GB.

The DiskManager will keep an eye on the directory, and remove the oldest sub-folder based on the last modification date if the directory size exceeds the user-defined maximum size.


The DiskManager was created based on the XFolderSize library from Hans Dietrich's implementation. Special acknowledgement to Hans Dietrich for XFolderSize.

Using the code

Here is the code:

class CDiskManager
 virtual ~CDiskManager();
 virtual BOOL GetFileSize(LPCTSTR szPath, PLARGE_INTEGER lpFileSize);
 virtual BOOL GetFolderSize(LPCTSTR szFolderPath, BOOL bRecursive, 
         BOOL bQuickSize, PLARGE_INTEGER lpFolderSize, 
         LPDWORD lpFolderCount = NULL, LPDWORD lpFileCount = NULL);
 virtual BOOL CleanupFolder(LPCTSTR szPath, LPDWORD lpFileCount = NULL);
 virtual BOOL GetDiskDetails(LPCTSTR szDrivePath, 
         PULARGE_INTEGER lpFreeSpace, PULARGE_INTEGER lpTotalSize);
 P_GFS pGetFileSizeEx;

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

How to use the code?

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

  1. You first need to add the following files to your project:
    • DiskManager.cpp
    • DiskManager.h
  2. In Visual Studio settings, select Not using pre-compiled header for DiskManager.cpp.
  3. Next, include the header file DiskManager.h in the source file where you want to use CDiskManager.
BOOL bSuccess = FALSE;
CDiskManager diskAgent;
LARGE_INTEGER liCurDirSize;  /**< Use 64-bits integer to prevent overflow */
bSuccess = diskAgent.GetFolderSize(szDir, TRUE, FALSE, &liCurDirSize);
   ULARGE_INTEGER liFreeDiskSpace, liTotalDiskSize, liSafeGuardSpace;
   __int64 uMaxSize = AfxGetApp()->GetProfileInt(_T("Settings"), 
              _T("MaxDiskSpaceForImageInGB"), 10);
   __int64 uDiskSpaceRequired = nFileCount * nFileSize;
   diskAgent.GetDiskDetails(szDir, &liFreeDiskSpace, &liTotalDiskSize);
   if(liFreeDiskSpace.QuadPart - uDiskSpaceRequired >0)
   /**< Ensure the logical drive has enough space */
    DWORD dwFolderCount=1;   
    while( (liCurDirSize.QuadPart + uDiskSpaceRequired) > 
              uMaxSize && dwFolderCount>0 )
    /**< Cleanup oldest folder if the total size of 
       image folder exceed the allowable(predefined) size  */
     diskAgent.GetFolderSize(szDir, TRUE, FALSE, 
           &liCurDirSize, &dwFolderCount);


CodeProject is always my best guru in helping me "survive" in the programming world. I am always thinking of how to contribute to this helpful society. I hope my first post in CodeProject will really help.

Please provide feedback if you have any comments for improvement. Thanks in advance.


  • 22-Jun-09: Initial creation.


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


About the Author

Software Developer (Senior)
Malaysia Malaysia
An ordinary guy from Penang Island...

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Mark Richards22-Dec-09 2:37
memberMark Richards22-Dec-09 2:37 
Generaljust some info Pin
flyingxu14-Dec-09 18:51
memberflyingxu14-Dec-09 18:51 

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
Web02 | 2.8.170813.1 | Last Updated 11 Dec 2009
Article Copyright 2009 by PenangDev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid