-yuelongr | 查看-50 | 发表时间-2009-12-1
下列代码演示了如何根据指定路径查找文件
头文件和所需链接库
#include <f32file.h> //include header
efsrv.lib // add in mmp.
按下列代码修改你的Container
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);
}
}
输出: 所有在C:/System/下的拥有后缀名.txt的文件将被存放在files(CDesCArrayFlat类型)中
使用时替换KPath及KFileName即可.(来自:Forum Nokia Wiki)