Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

如何查找文件

From Wiki
Jump to: navigation, search
Article Metadata

文章
翻译:
翻译自: Find Files
hoolee
最后由 hamishwillee 在 22 Dec 2011 编辑

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

头文件和所需链接库

#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类型)中

  • 使用时替换KPathKFileName即可.

This page was last modified on 22 December 2011, at 04:45.
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.

×