×
Namespaces

Variants
Actions

Find Files

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: kiran10182 (08 May 2007)
Last edited: hamishwillee (18 Sep 2012)

Following code snippet illustrates how to find files from the given path.

Header and library

#include <f32file.h> //include header
efsrv.lib // add in mmp.

Modify your container as follows:

void CYrContainer::FindFile()
{
_LIT(KPath,"C:\\System\\"); // Give your path here
_LIT(KFileName,"*.txt"); // Give file name here
RFs fs;
fs.Connect();
CleanupClosePushL(fs);
CDesCArrayFlat* files = new (ELeave) CDesCArrayFlat(5);
CleanupStack::PushL(files);
 
StartScanL(fs, KPath, KFileName, files); // Result will be stored in files array
 
CleanupStack::PopAndDestroy(files);
CleanupStack::PopAndDestroy(&fs);
}
 
void CYrContainer::StartScanL(
RFs aFs, const TDesC& aPath, const TDesC& aWild, CDesCArray* aArray)
{
CDirScan* DirScan = CDirScan::NewLC(aFs);
DirScan->SetScanDataL(aPath,
KEntryAttDir|KEntryAttMatchExclusive,
ESortNone, CDirScan::EScanDownTree);
 
while(1)
{
CDir* dir = NULL;
TRAPD(error, DirScan->NextL(dir));
if (error || !dir)
break;
 
delete dir;
 
ScanDirectoryL(aFs, DirScan->FullPath(), aWild, aArray);
};
CleanupStack::PopAndDestroy(DirScan);
}
 
void CYrContainer::ScanDirectoryL(
RFs& aFs, const TDesC& aDir, const TDesC& aWild, CDesCArray* aArray)
{
TParse parse;
parse.Set(aWild, &aDir, NULL);
TPtrC spec(parse.FullName());
 
TFindFile FindFile(aFs);
CDir* dir;
 
if (!FindFile.FindWildByPath(parse.FullName(), NULL, dir))
{
CleanupStack::PushL(dir);
 
for(TInt i = 0; i < dir->Count(); i++)
{
parse.Set((*dir)[i].iName, &spec, NULL);
aArray->AppendL(parse.FullName());
}
CleanupStack::PopAndDestroy(dir);
}
}

Output: All files under C:\System\ having extension .txt will be stored in an array files(of type CDesCArrayFlat).

  • Replace KPath and KFileName as per your requirements.

This page was last modified on 18 September 2012, at 08:55.
65 page views in the last 30 days.
×