![]() |
Desktop Development »
Shell and IE programming »
General
Advanced
Display content size for a folder in Explorer using shell extensionsBy Massimiliano ConteHow to add a column to Explorer to show the size of the content of folders |
VC6Win2K, MFC, ATL, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

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 stuffRemember 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; }
regsvr32 SHDireSizeColumn.dlland to uninstall, type: regsvr32 /u SHDireSizeColumn.dllRemember 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... :)
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Nov 2003 Editor: Nishant Sivakumar |
Copyright 2003 by Massimiliano Conte Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |