How to calculate directory content
Article Metadata
Class Header:
class TDirContentReader
{
public:
TDirContentReader( RFs& aFs, const TDesC& aDirName ):
iFs( aFs ), iDirName( aDirName ) {}
TInt Exec();
inline TUint DirsCount() { return iDirsCount; }
inline TUint FilesCount() { return iFilesCount; }
inline TInt64 FilesSize() { return iFileSize; }
private:
void ReadDirL( const TDesC& aDirName,
TUint& aDirsCount,
TUint& aFilesCount,
TInt64& aFileSize );
const TDesC& iDirName;
RFs& iFs;
TUint iDirsCount;
TUint iFilesCount;
TInt64 iFileSize;
};
Class Body:
TInt TDirContentReader :: Exec()
{
iDirsCount = 0;
iFilesCount = 0;
iFileSize = 0;
TInt execResult = KErrNone;
TRAP( execResult, ReadDirL( iDirName, iDirsCount, iFilesCount, iFileSize ) );
return execResult;
}
void TDirContentReader :: ReadDirL( const TDesC& aDirName,
TUint& aDirsCount,
TUint& aFilesCount,
TInt64& aFileSize )
{
CDir* fileList = NULL;
CDir* dirList = NULL;
User :: LeaveIfError(
iFs.GetDir( aDirName,
KEntryAttNormal | KEntryAttHidden | KEntryAttSystem,
ESortByName | EDirsFirst | EAscending,
fileList,
dirList ) );
aDirsCount += dirList->Count();
aFilesCount += fileList->Count();
for( TUint i = 0; i < fileList->Count(); i++ )
aFileSize += fileList->operator[](i).iSize;
delete fileList;
CleanupStack :: PushL( dirList );
for( TUint i = 0; i < dirList->Count(); i++ )
{
HBufC* buf = HBufC :: NewL( aDirName.Length() +
dirList->operator[](i).iName.Length() + 1);
TPtr ptr = buf->Des();
ptr.Append( aDirName );
ptr.Append( dirList->operator[]( i ).iName );
ptr.Append( KPathDelimiter );
CleanupStack :: PushL( buf );
ReadDirL( *buf, iDirsCount, iFilesCount, iFileSize );
CleanupStack :: PopAndDestroy( buf );
}
CleanupStack :: PopAndDestroy( dirList );
}
Usage example:
RFs myFs;
User::LeaveIfError( myFs.Connect() );
_LIT( KResources, "Z:\\resource\\" );
TDirContentReader dirContentReader( &myFs, KResources() );
if (dirContentReader.Exec() == KErrNone )
{
// TInt64 totalSize = dirContentReader.FilesSize();
}
myFs.Close();


I'm not keen on this code. It doesn't follow conventional naming, has some inefficiencies and passes RFs by pointer for some reason. It's good to have an example like this - it's requested regularly - but needs some tidying up.