×
Namespaces

Variants
Actions

List Files and Directories

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: vvsnaresh (21 Mar 2007)
Last edited: hamishwillee (29 Aug 2012)

The following code snippet shows how to get list of files and directories from a given drive(example C:\,D:\ etc).

File rfsengine.h with declaration of CRFsEngine is a part of FileBrowser example. You can find it in 3rd FP1 SDK onward.

#include <e32base.h>
#include <e32cons.h>
#include <e32debug.h>
#include <f32file.h>
#include "rfsengine.h"
 
_LIT(KFileBrowseTest, "FileBrowser");
_LIT(KCDrive, "C:\\");
_LIT(KPermissionDenied, "Permission Denied");
_LIT(KBackSlash, "\\");
 
 
GLDEF_C TInt E32Main()
{
// Create the cleanup stack
CTrapCleanup* cleanup = CTrapCleanup::New();
TRAPD(result, CallBrowseDirsL());
// Destroy the cleanup stack
delete cleanup;
 
return 0;
}
 
LOCAL_C void CallBrowseDirsL()
{
console = Console::NewL(KFileBrowseTest, TSize(KConsFullScreen, KConsFullScreen));
CleanupStack::PushL(console);
 
ListDirectoriesL(KCDrive);
 
CleanupStack::PopAndDestroy(); // close console
}
// Recursive function to list files and directories
LOCAL_C void ListDirectoriesL(const TDesC& aPath)
{
CRFsEngine* engine = CRFsEngine::NewL();
CleanupStack::PushL(engine);
 
RDebug::Print(aPath);
 
TInt result = engine->GetDirectoryAndFileList(aPath);
 
if (result==KErrNone)
{
TInt dirCount = engine->DirectoryCount();
TInt index = 0;
 
for (index=0; index<dirCount; index++)
{// Get directories
HBufC* subDirPath = SubDirPathLC(aPath, engine->DirectoryName(index));
ListDirectoriesL(*subDirPath);
CleanupStack::PopAndDestroy(subDirPath);
}
 
// List files in the directory
TInt fileCount = engine->FileCount();
index = 0;
for (; index<fileCount; index++)
{
RDebug::Print(engine->FileName(index));
}
}
else if (result==KErrPermissionDenied)
{
RDebug::Print(KPermissionDenied);
}
else
User::Leave(result);
 
CleanupStack::PopAndDestroy(engine);
}
 
 
// Allocates a heap buffer from aPath and aSubdir, with backslash terminator
LOCAL_C HBufC* SubDirPathLC(const TDesC& aPath, const TDesC& aSubDir)
{
HBufC* subDirBuf = HBufC::NewLC(aPath.Length() + aSubDir.Length() +1);
TPtr subDirPtr(subDirBuf->Des());
subDirPtr.Copy(aPath);
subDirPtr.Append(aSubDir);
subDirPtr.Append(KBackSlash);
return (subDirBuf);
}
This page was last modified on 29 August 2012, at 06:35.
43 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.

×