|

楼主 |
发表于 2012-10-24 10:57
|
显示全部楼层
其中,#include "getFiles.cpp"文件中的代码如下:
// use like this
/*
string path = "c:\\windows";
array collection;
//仅获取"ini"格式的文件
GetFiles(collection, path, ".ini");
//获取"ini", "txt"格式的文件
GetFiles(collection, path, ".ini | .txt");
*/
void GetFiles(array& files, string strPath, string strDot)
{
wkDb db;
array oDots = db.sqlSubsByDeli(strDot, "|");
for (int i = 0; i < oDots.getSize(); ++i)
{
oDots.trimLeft();
oDots.trimRight();
}
wkFile finder;
// build a string with wildcards
string strWildcard = strPath;
if (strWildcard.isEmpty())
return;
strWildcard.trimRight("\\");
strWildcard += "\\*.*";
// start working for files
bool bWorking = finder.findFile(strWildcard);
while (bWorking)
{
bWorking = finder.findNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
//Is dot? 4 -- dot
if (finder.findFileIs(4))
continue;
// if it's a directory, recursively search it
// 3 -- directory
if (finder.findFileIs(3))
{
// 2 -- file path
string ss = finder.findFileGet(2);
GetFiles(files, ss, strDot);
}
else
{
string s = finder.findFileGet(2);
int n = s.reverseFind(".");
if (n>0 && IsInDefineType(s.mid(n), oDots))
files.add(finder.findFileGet(2));
}
}
finder.findClose();
}
bool IsInDefineType(string s, array& o)
{
for (int i = 0; i < o.getSize(); ++i)
{
if (o.compareNoCase(s)==0)
return true;
}
return false;
}
|
|