如何根据指定路径查找文件

 下列代码演示了如何根据指定路径查找文件

头文件和所需链接库

#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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值