如何实现文件的搜索?

4water 2004-11-26 12:53:50
比如 我要把 *.exe的文件搜索出来,这样的代码 如何实现?能否提供相应的 代码?谢谢 !
...全文
481 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
51365133 2004-11-27
  • 打赏
  • 举报
回复
void dd::SearchFile(CString strDir,CString strFile)
{
CFileFind ff;
CString szDir=strDir;
if(szDir.Right(1)!="\\")
szDir+="*.*";
BOOL res==ff.FildNextFile();
while(res)
{
ff.FindNextFile();
if(ff.GetFileName()=strFile)
{
m_strFilesList.AddString(ff.GetFilePath());
}
if(ff.IsDirectory()&&!ff.IsDots())
{
SerchFile(ff.GetFilePath(),strFile);
}
}
ff.Close();
}
qrlvls 2004-11-27
  • 打赏
  • 举报
回复
WIN32_FIND_DATA dtFind;
HANDLE hFind;

hFind = FindFirstFile(_T("*.exe"), &dtFind);
...
FindNextFile(..
qrlvls 2004-11-27
  • 打赏
  • 举报
回复
用FindFile,FildNextFile来实现吧
4water 2004-11-27
  • 打赏
  • 举报
回复
我需要的 是在纯粹的SDK上,那该如何实现呢?
老夏Max 2004-11-26
  • 打赏
  • 举报
回复
strFile为路径,strFilter为文件的扩展名,如为EXE文件就设为“EXE”
老夏Max 2004-11-26
  • 打赏
  • 举报
回复
BOOL BrowseFile(CString strFile, CString strFilter, CStringList * pFileList, CStringList * pFileNameList, CStringList * pShowNameList)//查找文件函数
{
CFileFind FileFind;
CString SearchTarget = strFile;// + "\\" +strFilter;
if(SearchTarget.Right(1) != "\\")
SearchTarget += "\\";
SearchTarget += "*.*";
BOOL res = FileFind.FindFile(SearchTarget);
while(res)
{
res = FileFind.FindNextFile();
if(FileFind.IsDirectory() && !FileFind.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
CString strPath = FileFind.GetFilePath();
CString strTitle = FileFind.GetFileTitle();
BrowseFile(strPath, strFilter, pFileList, pFileNameList, pShowNameList);
}
else if(!FileFind.IsDirectory() && !FileFind.IsDots())
{
//显示当前访问的文件
CString strExtName;
CString strName;
strName = FileFind.GetFileName();
strExtName = strName.Right(strName.GetLength() - strName.Find('.')-1);
strExtName.MakeUpper();
if(!strExtName.CompareNoCase(strFilter))//扩展名正确
{
CString strPath = FileFind.GetFilePath();
pFileList->AddTail(strPath);
pFileNameList->AddTail(strName);
strName = strName.Left(strName.Find('.'));
pShowNameList->AddTail(strName);
}
}
}
FileFind.Close();//关闭
return TRUE;
}
4water 2004-11-26
  • 打赏
  • 举报
回复
先谢楼上的两位兄弟,我需要的 是在纯粹的SDK上,那该如何实现呢?
pleasehelpme 2004-11-26
  • 打赏
  • 举报
回复
// Find first file in specified directory
struct _finddata_t file;
long hwndFile = _findfirst(filePath, &file);
if (-1L == hwndFile) {
return CAN_NOT_FIND_FILE;
}
else
{
printf("%s", file.name);
// Find the rest of the files
while (0 == _findnext(hwndFile, &file ))
{
printf("%s", file.name);
}
_findclose( hwndFile );
return SUCCEED_RENAME;
}
老夏Max 2004-11-26
  • 打赏
  • 举报
回复
pFileList->找到的文件链表,含全路径和扩展名
pFileNameList->含扩展名的文件名
pShowNameList->仅仅为找到的文件名,不含扩展名
快乐鹦鹉 2004-11-26
  • 打赏
  • 举报
回复
用CFileFind进行搜索

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧