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

Tagged as

Go to top

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.

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:

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.
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)

Share

About the Author

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

Comments and Discussions

 
Question2Tb Virtual Disk Pinmembernhchmg27-Jun-11 20:27 
GeneralMy vote of 1 PinmemberMark Richards22-Dec-09 2:37 
Generaljust some info Pinmemberflyingxu14-Dec-09 18:51 

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 | Mobile
Web03 | 2.8.140905.1 | Last Updated 11 Dec 2009
Article Copyright 2009 by PenangDev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid