Click here to Skip to main content
Licence 
First Posted 18 Nov 2003
Views 109,011
Bookmarked 34 times

Display content size for a folder in Explorer using shell extensions

By | 18 Nov 2003 | Article
How to add a column to Explorer to show the size of the content of folders

Sample Image - SHDireSizeColumn.jpg

Introduction

Don't tell me you never had to right click a folder only to see in the properties the size of the content. If you never did it, skip reading this article!!!

Let's take a look at the code

If you did it, what you need is a shell extension!!! What is a shell extension? You will find the right answer reading The Michael Dunn's Complete Idiot's Guide to Writing Shell Extensions, here on CodeProject. What I did is read the Part VIII of the series and modify two functions to get my content-size-column in the Explorer. First of all, I did all the building without the last Platform SDK. So, I needed to add manually to my code the IColumnProvider interface declaration, plus some other declarations in my header files, like this
// remove all this stuff if you have the right shlobj.h file
// *** start stuff

typedef struct {
    GUID fmtid;
    DWORD pid;
} SHCOLUMNID, *LPSHCOLUMNID;
typedef const SHCOLUMNID* LPCSHCOLUMNID;


typedef struct {
    ULONG    dwFlags;
    ULONG    dwReserved;
    WCHAR    wszFolder[MAX_PATH];
} SHCOLUMNINIT, *LPSHCOLUMNINIT;

#define MAX_COLUMN_NAME_LEN 80
#define MAX_COLUMN_DESC_LEN 128

#pragma pack(1)
typedef struct {
    SHCOLUMNID  scid;
    VARTYPE     vt;
    DWORD       fmt;
    UINT        cChars;
    DWORD       csFlags; 
    WCHAR wszTitle[MAX_COLUMN_NAME_LEN];
    WCHAR wszDescription[MAX_COLUMN_DESC_LEN];
} SHCOLUMNINFO, *LPSHCOLUMNINFO;


#define SHCDF_UPDATEITEM        0x00000001      

typedef struct {
    ULONG   dwFlags;
    DWORD   dwFileAttributes;
    ULONG   dwReserved;
    WCHAR   *pwszExt;
    WCHAR   wszFile[MAX_PATH];
} SHCOLUMNDATA, *LPSHCOLUMNDATA;
typedef const SHCOLUMNDATA* LPCSHCOLUMNDATA;


DECLARE_INTERFACE_(IColumnProvider, IUnknown)
{
    // IUnknown methods
    STDMETHOD (QueryInterface)(THIS_ REFIID riid, void **ppv) PURE;
    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
    STDMETHOD_(ULONG, Release)(THIS) PURE;

    // IColumnProvider methods
    STDMETHOD (Initialize)(THIS_ LPSHCOLUMNINIT psci) PURE;
    STDMETHOD (GetColumnInfo)(THIS_ DWORD dwIndex, LPSHCOLUMNINFO psci) PURE;
    STDMETHOD (GetItemData)(THIS_ LPCSHCOLUMNID pscid, LPCSHCOLUMNDATA pscd,
VARIANT *pvarData) PURE;
};

// *** end stuff
Remember to remove all this code (in DirSizeColumn.h) if you have the right Platform SDK or you'll get some compiler error. The core of the dll are the two functions CDirSizeColumn::GetColumnInfo and CDirSizeColumn::GetItemData. The first tells Explorer that there will be a new column, right aligned etc etc:
STDMETHODIMP CDirSizeColumn::GetColumnInfo ( DWORD dwIndex, 
    LPSHCOLUMNINFO psci )
{
    HRESULT hRes=S_FALSE;

    if (dwIndex==0)
    {
        psci->scid.fmtid = *_Module.pguidVer;   
        psci->scid.pid   = MY_COLUMN_ID;                               
        psci->vt         = VT_LPSTR;                                        
        psci->fmt        = LVCFMT_RIGHT;                                  
        psci->csFlags    = 0x22; // this is  SHCOLSTATE_TYPE_INT | 
                                 // SHCOLSTATE_SLOW
        psci->cChars     = 6;
        lstrcpyW ( psci->wszTitle, L"Content Size\0");
        lstrcpyW ( psci->wszDescription, 
             L"Size of all files and subfolders contained\0");
                            
        hRes=S_OK;
    }

    return hRes;
}
The second one will do the calc stuff for any directory being passed as param
STDMETHODIMP CDirSizeColumn::GetItemData (
    LPCSHCOLUMNID   pscid,
    LPCSHCOLUMNDATA pscd,
    VARIANT*        pvarData )
{
    HRESULT hRes=S_FALSE;

    // is my module?
    if ( pscid->fmtid == *_Module.pguidVer )
    {
        // is a directory?
        if ( (pscd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==
            FILE_ATTRIBUTE_DIRECTORY)
        {
            // is my column?
            if ( pscid->pid == MY_COLUMN_ID )
            {
                CString strFileName(pscd->wszFile);

                char szText[100];
                sprintf(szText,"%I64d KB",(__int64)(
                     GetDirSize(strFileName)/(__int64)1024));

                CComVariant vData(szText);

                vData.Detach ( pvarData );

                hRes=S_OK;
            }
        }
    }

    return hRes;
}
Really simple, isn't it? :) The function that read folders size is a simple recursive function, uses CFileFind from MFC and iterate in any subfolder and any file in a given folder.
__int64 GetDirSize(CString strFileName)
{
    __int64 i64Size=0;

    TRY
    {
        CFileFind finder;
        BOOL bWorking = finder.FindFile(strFileName+"\\*.*");

        while (bWorking)
        {
            bWorking = finder.FindNextFile();

            // check for dots
            if (!finder.IsDots())
            {
                // check for recursion
                if (finder.IsDirectory())
                    i64Size=i64Size+GetDirSize(finder.GetFilePath());
                else
                    i64Size=i64Size+finder.GetLength64();
            }
        }
    }
    CATCH(CException, ex)
    {
        // catch any error here

        i64Size=0;
    }
    END_CATCH

    return i64Size;
}

Install/Uninstall

To install the extension use the following command:
regsvr32 SHDireSizeColumn.dll
and to uninstall, type:
regsvr32 /u SHDireSizeColumn.dll
Remember to close all Explorer windows and next time you want to know the size of the content of a dir you will have to right click etc etc etc... :)

And then...

Many many thanks to Michael Dunn, writing this article has been a matter of minutes once I read his guides!!! :) See you soon.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Massimiliano Conte

Software Developer (Senior)
Selex SI
Italy Italy

Member

Hi Smile | :)
I was born in 1970 (Augusta - Italy).
I live in Taranto - Italy.
I work in Taranto - Italy.
I like computer science!!!
That's all!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCan't debug :( ? Pinmembertermal6:13 18 Jun '08  
When i try to debug this project i recive this message:
Debugging information for "explorer.exe" cannot be found or does not match
Any idea how to solve this? I use VS8!
 
Thanks for any help!
termal
GeneralDoesn't work on Vista :( Pinmemberkmst15:02 10 Jun '08  
GeneralVisual C++ 2008 Express missing afxwin.h Pinmemberprader21:47 23 May '08  
GeneralSHDireSizeColumn_demo does not work! PinmemberCdD9814:35 21 Nov '04  
GeneralRe: SHDireSizeColumn_demo does not work! PinmemberKhan-Dam060:48 25 Oct '06  
Questioncan we add image bar also in column? PinmemberAJAYP0:28 31 Aug '04  
GeneralAnother Newbie Question PinmemberJackboy6:23 17 Jul '04  
GeneralIs the new column necessary - Part II PinmemberRage2:57 7 May '04  
GeneralRe: Is the new column necessary - Part II PinmemberAnthony_Yio0:24 3 May '06  
GeneralSorting ability with size in KB, MB, GB... PinsussAnonymous15:58 10 Apr '04  
GeneralRe: Sorting ability with size in KB, MB, GB... Pinmembersazerty12:13 5 Jul '04  
GeneralCached & realtime updated (NTFS only)! PinmemberMark Porsby3:13 7 Dec '03  
GeneralCompiler Errors Pinmemberbhuvan013:47 26 Nov '03  
GeneralRe: Compiler Errors PinmemberSharan Basappa22:09 27 Nov '03  
GeneralRe: Compiler Errors Pinmemberamauta8:39 17 Jul '04  
GeneralReading Pinmemberespelly1:58 21 Nov '03  
GeneralPerformance question Pinmemberdog_spawn8:32 20 Nov '03  
GeneralRe: Performance question PinmemberJonathanLivingstone21:13 20 Nov '03  
GeneralRe: Performance question Pinmembergebrudergrimm14:03 26 Nov '03  
GeneralI just use Total Commander for this ;) PinmemberKochise23:24 19 Nov '03  
GeneralCheck out Directory Opus as well. PinmemberLeo Davidson3:02 21 Nov '03  
GeneralCompiler errors. PinmemberWREY22:30 19 Nov '03  
QuestionIs new column necessary? PinmemberHans Dietrich22:12 19 Nov '03  
AnswerRe: Is new column necessary? PinmemberJonathanLivingstone23:02 19 Nov '03  
GeneralRe: Is new column necessary? PinmemberHans Dietrich1:54 20 Nov '03  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 19 Nov 2003
Article Copyright 2003 by Massimiliano Conte
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid