Click here to Skip to main content
15,887,746 members
Articles / Desktop Programming / ATL

Folder Size Information in the Windows Explorer Details View

Rate me:
Please Sign up or sign in to vote.
4.59/5 (76 votes)
18 Aug 2010CPOL2 min read 444.8K   13.8K   80   96
This code displays the folder size information in the Explorer's Details view.
Sample Image - DirSize.jpg

Introduction

In day to day life, when one wants to know the size of a folder, (s)he has to right click on the folder name and has to choose the Properties. Using this DLL, one can view the folder size as one of the columns of the Explorer as shown in the image. We also have the facility to sort according to the folder size. (I did not do anything to sort, it is done by Explorer. :))

This class implements the Shell interface called IColumnProvider. Using this interface, one can customize the Explorer's Details view. This class adds one more column to the Details view, which displays the consolidated folder size.

To build this project, you must install Microsoft Platform SDK, or click here [53.5 KB] to download SHLOBJ.h.

How to Use the DLL

Well, if you build the project, you need not do anything, since the IDE automatically registers the DLL. If demo project is downloaded, then unzip the DLL and register it using the command "regsvr32 <Path of the unzipped DLL>". E.g., Regsvr32 c:\DirSize.DLL, if the DLL is present in C:.

Scope for Further Improvements

This code can be modified to display the number of files/folders in the folder. Or maybe, you can pack two more classes to display columns for number of files and number of folders into the same DLL to do so.

Performance Issues

When the user selects the "Folder Size" item from the Explorer context menu, the Explorer starts calculating the folder size by traversing all the files and nested folders. First time, it might take some minutes depending upon the contents of the folder. But all the operations are done in the background (Explorer is smart enough!.. Ehh). So this won't stop you from traversing through other files.

License

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


Written By
Team Leader
India India
He is a graduate, currently working as a Software Engineer in Bangalore, India. You can reach him at sharan34@yahoo.com

Comments and Discussions

 
QuestionRe: Show size in MB Pin
Lonesan15-Jun-07 15:02
Lonesan15-Jun-07 15:02 
GeneralRe: Show size in MB Pin
robaxn16-Jul-05 23:27
robaxn16-Jul-05 23:27 
GeneralRe: Show size in MB Pin
JURAhasiok28-Aug-06 12:46
JURAhasiok28-Aug-06 12:46 
QuestionRe: Show size in MB Pin
Cosmo2b28-Jan-07 20:49
Cosmo2b28-Jan-07 20:49 
GeneralColumn alignment Pin
Claudius Mokler24-May-04 0:19
Claudius Mokler24-May-04 0:19 
GeneralSuggestion... Pin
dandy727-May-04 7:20
dandy727-May-04 7:20 
GeneralRe: Suggestion... Pin
m_harriss23-May-04 0:14
m_harriss23-May-04 0:14 
GeneralRe: Suggestion... Pin
m_harriss23-May-04 6:55
m_harriss23-May-04 6:55 
i make many changes:
-1)add a GetFileSize() function;
-2)give a number gap of 1000, like 3,234,122;
-3)use 2 kinds of unit to make difference, K" for folders, KB for files;
-4)use "files + folders <200" as recusive limit, and add '~' just for large scale folders, it gets much faster;
-5)text fmt is now set to "right" side;
....so "size" column now looks like:
```````````````````~21,813 K"| folder
`````````````````````1,332 KB| file
I put the whole file of "colhandler.cpp", header file is easy to edit.

==========================================================
<small>// ColHandler.cpp : Implementation of CColHandler<br />
#include "stdafx.h"<br />
#include "DirSize.h"<br />
#include "ColHandler.h"<br />
<br />
<br />
// This method has to be implemented since it is called by the shell.<br />
// we are not doing anything here, so just return success.<br />
STDMETHODIMP CColHandler::Initialize(LPCSHCOLUMNINIT psci)<br />
{<br />
	return S_OK;<br />
}<br />
<br />
// This method is called by the shell to get the column information that is going<br />
// to be displayed. This function will decide the name of the column and the type<br />
// of the data that it is holding. It also tell the shell that the computation of <br />
// the folder size is slow hence it has to be computed in background.<br />
STDMETHODIMP CColHandler::GetColumnInfo(DWORD dwIndex, SHCOLUMNINFO *psci)<br />
{<br />
	TCHAR szText[] = "size";<br />
	<br />
	psci->scid.fmtid.Data1 = 0x28636AA6L;<br />
	psci->scid.fmtid.Data2 = 0x953D;<br />
	psci->scid.fmtid.Data3 = 0x11D2;<br />
	strcpy((char *)psci->scid.fmtid.Data4,"B5D600C04FD918D0");<br />
	<br />
	psci->scid.pid = 12;<br />
	psci->vt = VT_BSTR;<br />
	psci->fmt = LVCFMT_RIGHT;/////////////////////<br />
	psci->cChars = 35;<br />
	psci->csFlags = SHCOLSTATE_TYPE_STR|SHCOLSTATE_SLOW;<br />
	<br />
	<br />
	MultiByteToWideChar( CP_ACP, 0, szText,<br />
		strlen(szText)+1, psci->wszTitle,   <br />
		sizeof(psci->wszTitle)/sizeof(psci->wszTitle[0]) );<br />
	<br />
	MultiByteToWideChar( CP_ACP, 0, szText,<br />
		strlen(szText)+1, psci->wszDescription,   <br />
		sizeof(psci->wszDescription)/sizeof(psci->wszDescription[0]) );<br />
	<br />
	if(dwIndex == 0)<br />
	{<br />
		return S_OK;<br />
	}<br />
	else<br />
	{<br />
		return S_FALSE;<br />
	}<br />
}<br />
<br />
// The shell calls this method for each item in the view and sends the folder path in 'pscd'<br />
// This folder path is used to get the size of that folder. The size of the folder is converted<br />
// to string and then sent to the shell through a VARIANT.<br />
STDMETHODIMP CColHandler::GetItemData(LPCSHCOLUMNID pscid, LPCSHCOLUMNDATA pscd, VARIANT *pvarData)<br />
{<br />
	USES_CONVERSION;<br />
	V_VT(pvarData) = VT_BSTR;<br />
	DWORD dwSize = 0;<br />
	char szTmp[200],sz_[200];<br />
	DWORD dwFiles = 0, dwFolders = 0;<br />
	BOOL bLimit=false;<br />
	<br />
	DWORD64 size = 0;<br />
	if(	pscd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY  )<br />
		dwSize = GetFolderSize(OLE2T(pscd->wszFile),&dwFiles,&dwFolders,&bLimit);<br />
	//	dwSize=102400000;//½ÚԼʱ¼ä<br />
	else<br />
	{<br />
		dwSize = GetFileSize(OLE2T(pscd->wszFile),&dwFiles,&dwFolders);<br />
	//	V_BSTR(pvarData) = SysAllocString(T2OLE(OLE2T(pscd->wszFile)));<br />
	//	return S_OK;<br />
	}<br />
<br />
//	size = dwSize/(1024*1024);<br />
//	if(size == 0)<br />
	{<br />
		size = dwSize/1024;<br />
		if(dwSize%1024)<br />
			size++;<br />
	//	if(size == 0)<br />
	//	{<br />
	//		_i64toa(dwSize,szTmp,10);<br />
    //      strcat(szTmp," Bytes");<br />
	//	}<br />
	//	else<br />
	//	{<br />
			_i64toa(size,szTmp,10);<br />
<br />
			int len=strlen(szTmp);<br />
			int index=0;<br />
			memset(sz_,0,200);<br />
			if(bLimit)<br />
			{<br />
				strcat(sz_,"~");///////////////////<br />
				index+=1;<br />
			}<br />
<br />
			int iTmp=len%3;<br />
			if(iTmp!=0)<br />
			{<br />
				memcpy(sz_+index,szTmp,iTmp);<br />
				index+=iTmp;<br />
				len-=iTmp;<br />
<br />
				if(len>0)<br />
				{<br />
					strcat(sz_,",");<br />
					++index;<br />
				}<br />
			}<br />
<br />
			while(len>0)<br />
			{<br />
				memcpy(sz_+index,szTmp+iTmp,3);<br />
				index+=3;iTmp+=3;len-=3;<br />
				if(len>0)<br />
				{<br />
					strcat(sz_,",");<br />
					++index;<br />
				}<br />
			}<br />
			if(	pscd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY  )<br />
					strcat(sz_," K\"");<br />
			else	strcat(sz_," KB");<br />
	//	}<br />
	}<br />
//	else<br />
//	{<br />
//		_i64toa(size,szTmp,10);<br />
//		strcat(szTmp," MB");<br />
//	}<br />
	<br />
	V_BSTR(pvarData) = SysAllocString(T2OLE(sz_));<br />
	<br />
	return S_OK;<br />
}<br />
<br />
<br />
// This function is the private to this class. It is called with the folder path. It <br />
// returns the size of the folder. This is made recursive to calculate the size of the <br />
// nested folder(s) if any.<br />
DWORD CColHandler::GetFolderSize(LPCTSTR szPath, DWORD *dwFiles, DWORD *dwFolders,BOOL *bLimit)<br />
{<br />
	TCHAR szFileFilter[512];<br />
	TCHAR szFilePath[512];<br />
	HANDLE hFind = NULL;<br />
	WIN32_FIND_DATA fileinfo;<br />
	DWORD    dwSize = 0;<br />
	<br />
	strcpy(szFilePath,szPath);<br />
	strcat(szFilePath,"\\");<br />
	strcpy(szFileFilter,szFilePath);<br />
	strcat(szFileFilter,"*.*");<br />
	<br />
	<br />
	hFind = FindFirstFile(szFileFilter,&fileinfo);<br />
	do<br />
	{<br />
		if(fileinfo.dwFileAttributes &	FILE_ATTRIBUTE_DIRECTORY )<br />
		{<br />
			if (!strcmp(fileinfo.cFileName,".") || !strcmp(fileinfo.cFileName,".."))<br />
			{<br />
				//Do nothing for "." and ".." folders<br />
			}<br />
			else<br />
			{<br />
				TCHAR sztmp[512];<br />
				strcpy(sztmp,szFilePath);<br />
				strcat(sztmp,fileinfo.cFileName);<br />
				dwSize = dwSize + GetFolderSize(sztmp,dwFiles,dwFolders,bLimit);<br />
				if(dwFolders != NULL)<br />
					++(*dwFolders);<br />
			}<br />
		}<br />
		else<br />
		{<br />
			if(dwFiles != NULL)<br />
				++(*dwFiles);<br />
		}<br />
		<br />
		dwSize += fileinfo.nFileSizeLow;<br />
		if(dwFolders && dwFiles && bLimit)<br />
		{<br />
			if(*dwFolders+*dwFiles>200)<br />
				*bLimit=true;<br />
		}<br />
	}while(FindNextFile(hFind,&fileinfo)&& ((bLimit!=NULL && !(*bLimit))||!bLimit) );<br />
	<br />
	FindClose(hFind);<br />
	return dwSize;<br />
	<br />
}<br />
<br />
DWORD CColHandler::GetFileSize(LPCTSTR szPath, DWORD *dwFiles, DWORD *dwFolders)<br />
{<br />
	WIN32_FIND_DATA fileinfo;<br />
	HANDLE hFind = FindFirstFile(szPath,&fileinfo);<br />
	FindClose(hFind);<br />
	return fileinfo.nFileSizeHigh * (MAXDWORD+1) + fileinfo.nFileSizeLow;<br />
}</small>

=====================================================
GeneralRe: Suggestion... Pin
Anonymous22-Jul-04 10:46
Anonymous22-Jul-04 10:46 
GeneralNo extra Column after registering dll Pin
Sorri6-May-04 23:45
Sorri6-May-04 23:45 
GeneralRe: No extra Column after registering dll Pin
Sorri6-May-04 23:48
Sorri6-May-04 23:48 
GeneralRe: No extra Column after registering dll Pin
Mr. GD (Mr.Green)19-Sep-16 6:13
professionalMr. GD (Mr.Green)19-Sep-16 6:13 
Questionhow do I uninstall Pin
Anonymous23-Apr-04 6:14
Anonymous23-Apr-04 6:14 
AnswerRe: how do I uninstall Pin
Poojas29-Apr-04 9:38
Poojas29-Apr-04 9:38 
GeneralRe: how do I uninstall Pin
Anonymous10-May-04 5:42
Anonymous10-May-04 5:42 
AnswerRe: how do I uninstall Pin
TrInAdOr26-Jul-05 5:13
TrInAdOr26-Jul-05 5:13 
GeneralWindows Explorer (Search Files &amp; Folders) Pin
Anonymous17-Apr-04 11:33
Anonymous17-Apr-04 11:33 
GeneralWindows 98 Pin
John T6-Apr-04 7:25
John T6-Apr-04 7:25 
GeneralRe: Windows 98 Pin
Sharan Basappa6-May-04 18:02
Sharan Basappa6-May-04 18:02 
GeneralRe: Windows 98 Pin
rchas9-Feb-05 15:35
rchas9-Feb-05 15:35 
GeneralError Code Pin
Diamondblack22-Mar-04 5:06
Diamondblack22-Mar-04 5:06 
QuestionHow to install... Pin
Rajparikh2-Feb-04 22:45
Rajparikh2-Feb-04 22:45 
AnswerRe: How to install... Pin
Sharan Basappa9-Feb-04 0:06
Sharan Basappa9-Feb-04 0:06 
GeneralRe: How to install... Pin
upcleecher16-Mar-04 7:04
upcleecher16-Mar-04 7:04 
GeneralRe: How to install... Pin
tricotalazan28-Mar-04 1:29
tricotalazan28-Mar-04 1:29 

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.