求助,CFileFind遍历子目录下所有文件的问题

BreezeChen 2007-10-15 03:26:38
CFileFind finder;
int work=finder.FindFile("*.jpg");

while(Work)
{
Work=finder.FindNextFile();
char FileName[250];
sprintf(FileName,"%s",finder.GetFilePath());
family[picnum]=FileName;
picnum+=1;
}

我现在用以上的代码能把目录A下的所有图片读上来,请问我如果想获得包括子目录的图片该怎么改啊
...全文
591 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
51365133 2007-10-15
  • 打赏
  • 举报
回复
void CBrowsDirDlg::BrowsDir(CString strDir)
{
CFileFind ff;
CString szDir=strDir;
if(szDir.Right(1) != "\\")
szDir+="\\";

szDir+="*.*";

Doevents();

BOOL res=ff.FindFile(szDir);
while(res)
{
Doevents();
res=ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
BrowsDir(ff.GetFilePath());
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
CString str;

str.Format("%s",ff.GetFilePath());
if(str.GetLength()>40)
str=str.Left(20)+"...\\"+ff.GetFileName();
//显示当前访问的文件

CStatic* p=(CStatic*)GetDlgItem(IDC_STATIC_FILE);
p->SetWindowText(str);
Sleep(50);
}
}
ff.Close();
}
//开辟线程避免假死状态
void CBrowsDirDlg::Doevents()
{
MSG msg;
if (PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
miaoshengwu 2007-10-15
  • 打赏
  • 举报
回复
//是否已建立文件夹,如果建立,则返回TRUE,反之,返回FALSE
BOOL finddir::FindDirectory(const CString dt, CString str_dir)
{
// 查找是否存在用户文件夹,已建立:true 未建立:false
SetCurrentDirectory(dt);
CFileFind FileFind;
BOOL bResult, aResult;
CString str_temp;

aResult = FALSE;
bResult = FileFind.FindFile();
while(bResult){
bResult = FileFind.FindNextFile();
if( FileFind.IsDots() ) continue;
if( FileFind.IsDirectory() ){
FindDirectory( FileFind.GetFilePath() ,str_dir);
}
}
FileFind.Close();
return aResult;
}

根据需要自己再改改吧
guo_wei 2007-10-15
  • 打赏
  • 举报
回复
做个递归函数,如果finder.IsDirectory(),就继续搜索这个子目录
凤矶 2007-10-15
  • 打赏
  • 举报
回复
ReadFiles(CString szPath)
{ CFileFind ff;
DWORD size = 0;
CString szDir = szPath + _T("\\*.*"); //搜索路径,包括所有子目录
BOOL ret = ff.FindFile(szDir);

while (ret)
{
ret = ff.FindNextFile();

if(!ff.IsDots())
{
if(ff.IsDirectory())
{
//子目录结点,递归ReadFiles(ff.GetFilePath());
}
else
{
ReadFile(ff.GetFilePath());// 这个你自己写
}

}

}

ff.Close();
}
/** ===================================================== 功能: CFolderContent类搜索一个目录, 列出该目录下的所有目录名称,列出所有子目录下的文件名称等属性。 作者: jef 作者邮箱: dungeonsnd@126.com 发步时间: 20100311 版本: v1.1 版权: 请遵循GNU. 对外接口: int GetAllSub(CString csPath) csPath: 一个目录或者一个完整的文件名 使用举例: void CGetFolderContentView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CString cs,csSaveFileName,csFileSave,csT,csT1; cs ="C:\\Documents and Settings\\All Users\\Documents\\My Music"; char chModule[8192]; memset(chModule,0,8192); GetModuleFileName(NULL,chModule,8192); csT.Format("%s",chModule); csT =csT.Left( csT.ReverseFind(_T('\\')) ); csSaveFileName =csT+_T("\\FolderContent输出文件.txt"); CRect rtClient; GetClientRect(&rtClient); CClientDC dc(this); dc.SetTextColor(RGB(200,80,80)); dc.FillSolidRect(rtClient,RGB(240,240,240)); CFolderContent fc; fc.GetAllSub(cs); int i,len,k; csT ="-------"; csT =cs+"总大小为"; csT1.Format(" %.4fMB,清单文件已保存在 %s",fc.m_dTotalSize/double(1024*1024),csSaveFileName); csT +=csT1; csT +="-------"; dc.TextOut(5,0,csT); csFileSave +=csT+_T("\r\n"); csT ="-------"; csT +="所有子目录如下:"; csT +="-------"; dc.TextOut(5,25,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_Directory.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecDirectorySize[i]/double(1024*1024)); csT1.Format(" 创建时间:%d年%d月%d日 ", fc.m_vecFileLastCreationTime[i].wYear, fc.m_vecFileLastCreationTime[i].wMonth, fc.m_vecFileLastCreationTime[i].wDay); csT =csT+csT1; csT =fc.m_Directory.GetAt(i)+csT; dc.TextOut(5,(i+2)*20,csT); csFileSave +=csT+_T("\r\n"); } k =i; csT ="-------"; csT +="所有文件如下:"; csT +="-------"; dc.TextOut(5,(k+3)*20,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_PathNameExt.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecFileSize[i]/double(1024*1024)); csT1.Format(" 修改时间:%d年%d月%d日 ", fc.m_vecFileLastWriteTime[i].wYear, fc.m_vecFile

16,472

社区成员

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

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

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