专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C++ Builder

在Edit1中给出"cpp"等后缀名,应该如何样在系统中搜索出所有的cpp文件,并显示到ListBox1中呢

发布时间:2011-06-28 13:31:40 文章来源:www.iduyao.cn 采编人员:星星草
在Edit1中给出"*.cpp"等后缀名,应该怎么样在系统中搜索出所有的cpp文件,并显示到ListBox1中呢?
在Edit1中给出 "*.cpp "等后缀名,应该怎么样在系统中搜索出所有的cpp文件,并显示到ListBox1中呢?     请高手指点迷津!!!

------解决方案--------------------
struct ffblk ffblk;
int done;
done = findfirst( "*.cpp ",&ffblk,0);
while (!done)
{
Memo2-> Lines-> Add(AnsiString(ffblk.ff_name));
done = findnext(&ffblk);
}
done = findfirst( "*.h ",&ffblk,0);
while (!done)
{
Memo2-> Lines-> Add(AnsiString(ffblk.ff_name));
done = findnext(&ffblk);
}
int i, j;
j = Memo2-> Lines-> Count;
if (j == 0)
{
MessageBox(Handle, "该目录中没有C++源文件 ", "C++源文件信息提取 ",MB_OK | MB_ICONHAND);
return;
}
------解决方案--------------------
把_FindFileProc改写一下,支持通配符,完整代码:

bool g_bStopFlag;

//---------------------------------------
// 文件搜索函数(递归)
void _FindFileProc(String strFindFile, String strDir, TStrings *pList);
// 显示信息
void _ShowProcess(String strMsg);
// 用户调用的查找文件主函数
void CrnFindFile(String strFindFile, TStrings *pList);
//---------------------------------------
void _FindFileProc(String strFindFile, String strDir,
TStrings *pList)
{
WIN32_FIND_DATA wfd;
if(strDir.LastDelimiter( "\\ ") != strDir.Length())
strDir += "\\ ";
String strFile( " ");

HANDLE hFind;
// 查找该目录下的文件,支持通配符
hFind = FindFirstFile((strDir + strFindFile).c_str(), &wfd);
if(INVALID_HANDLE_VALUE != hFind)
{
do
{
Application-> ProcessMessages();
if(g_bStopFlag)
break;
strFile = String(wfd.cFileName);
_ShowProcess(strDir + strFile);
if(strFile == ". " || strFile == ".. ")
continue;

pList-> Add(strDir + String(wfd.cFileName));
}while(FindNextFile(hFind, &wfd));
}
FindClose(hFind);

// 查找子目录
hFind = FindFirstFile((strDir + "*.* ").c_str(), &wfd);
if(INVALID_HANDLE_VALUE != hFind)
{
do
{
Application-> ProcessMessages();
if(g_bStopFlag)
break;

strFile = String(wfd.cFileName);
if(strFile == ". " || strFile == ".. ")
continue;

// 如果是子目录就进入子目录搜索
if((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
== FILE_ATTRIBUTE_DIRECTORY)
{
_ShowProcess(strDir + strFile);
_FindFileProc(strFindFile, strDir + strFile + "\\ ", pList);
}
}while(FindNextFile(hFind, &wfd));
}
FindClose(hFind);
}
//---------------------------------------
void _ShowProcess(String strMsg)
{
Form1-> Caption = strMsg;
}
//---------------------------------------
void CrnFindFile(String strFindFile, TStrings *pList)
{
char szDrvBuf[100]; // 用于存放各盘的逻辑盘符的缓冲区
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: