Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C++

DiskManger - GetFolderSize and CleanupFolder

Rate me:
Please Sign up or sign in to vote.
4.33/5 (9 votes)
10 Dec 2009CPOL2 min read 29K   560   27   2
A class to check disk information like total size, free space, used space, and clean up.

Introduction

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.

Background

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:

C++
class CDiskManager
{
public:
 CDiskManager();
 virtual ~CDiskManager();
public:
 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.
C++
BOOL bSuccess = FALSE;
CDiskManager diskAgent;
LARGE_INTEGER liCurDirSize;  /**< Use 64-bits integer to prevent overflow */
bSuccess = diskAgent.GetFolderSize(szDir, TRUE, FALSE, &liCurDirSize);
if(bSuccess)
{ 
   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.CleanupFolder(szDir);
     diskAgent.GetFolderSize(szDir, TRUE, FALSE, 
           &liCurDirSize, &dwFolderCount);
    } 
}
...

Disclaimer

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.

History

  • 22-Jun-09: Initial creation.

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)
Malaysia Malaysia
An ordinary guy from Penang Island...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Mark Richards22-Dec-09 2:37
Mark Richards22-Dec-09 2:37 
Generaljust some info Pin
flyingxu14-Dec-09 18:51
flyingxu14-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.