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:
- You first need to add the following files to your project:
- DiskManager.cpp
- DiskManager.h
- In Visual Studio settings, select Not using pre-compiled header for DiskManager.cpp.
- 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;
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)
{
DWORD dwFolderCount=1;
while( (liCurDirSize.QuadPart + uDiskSpaceRequired) >
uMaxSize && dwFolderCount>0 )
{
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.