首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

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

2012-01-29 
求助,CFileFind遍历子目录下所有文件的问题CFileFind finderint workfinder.FindFile(*.jpg)while(Wo

求助,CFileFind遍历子目录下所有文件的问题
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下的所有图片读上来,请问我如果想获得包括子目录的图片该怎么改啊

[解决办法]
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(); 
}
[解决办法]
做个递归函数,如果finder.IsDirectory(),就继续搜索这个子目录
[解决办法]
//是否已建立文件夹,如果建立,则返回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;
}

根据需要自己再改改吧
[解决办法]
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);
}
}

热点排行