×
Namespaces

Variants
Actions

How to calculate directory content

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: Den123 (26 Jun 2007)
Last edited: hamishwillee (30 May 2013)

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();
This page was last modified on 30 May 2013, at 07:38.
31 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×