怎么遍历一个目录下的所有子目录,寻找我要的文件 FindFirstFile, FindNextFile, FindClose 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 BOOL DeleteDir(LPCTSTR pstr){ CString dirall(pstr); dirall+="\\*.*"; CFileFind finder; BOOL bFind=(BOOL)finder.FindFile(dirall); while(bFind) { bFind=finder.FindNextFile(); if(!finder.IsDots()) { CString str = finder.GetFilePath(); if(finder.IsDirectory()) { DeleteDir(str); } else { DeleteFile(str); } } } finder.Close(); if(!RemoveDirectory(pstr)) { AfxMessageBox("删除文件目录出错!"); return FALSE; } return TRUE;}修改一下 http://www.csdn.net/expert/topic/461/461194.xml?temp=.4256861 BOOL b; //定义一个布尔值,当所有的文件都遍历过之后,停止搜索CFileFind cf;int nItem;CString fName,fvideo,ftitle,fstime,fetime,fsize;CFileStatus status;CTime fctime,fmtime;double filesize;_chdir(lzPath); //进入指定目录,lzPath是指定的路径。 b= cf.FindFile("*.*", 0); //执行通配符(*.*)的搜索,为了派生类的FindFile实现多态保留,后一个参数必须为0 strcpy(strPath,lzPath); while (b) { b=cf.FindNextFile(); //继续前次调用FindFile生成的文件搜索。如果有更多的文件,则返回非0值;如果是最后一个文件,并且先前FindFile或FindNextFile的调用都返回非0,则返回值为0 fName = cf.GetFileName(); //获得找到文件的文件名 ftitle = cf.GetFileTitle(); //得到指定文件的标题 CFile::GetStatus(fName,status); fctime = status.m_ctime; //得到文件的创建时间fstime.format("%.4d-%.2d-%.2d%.2d:%.2d:%.2d",fctime.GetYear(),fctime.GetMonth(), fctime.GetDay(),fctime.GetHour(),fctime.GetMinute(),fctime.GetSecond()); fmtime = status.m_mtime; //得到文件最后修改时间fetime.format((“.4d-%.2d-%.2d%.2d:%.2d:%.2d",fmtime.GetYear(),fmtime.GetMonth(), fmtime.GetDay(),fmtime.GetHour(),fmtime.GetMinute(),fmtime.GetSecond()); filesize =(double) (lzStatus.m_size)/1024; //文件大小fsize.format("%.3lfKB",filesize); //在列表视图中显示nItem = m_list.InsertItem(count,fName); m_list.SetItem(nItem,1,1,fvideo,NULL,0,0,0); m_list.SetItem(nItem,2,1,fstime,NULL,0,0,0); m_list.SetItem(nItem,3,1,fetime,NULL,0,0,0); m_list.SetItem(nItem,4,1,fsize,NULL,0,0,0); }这是我做的一个程序,遍历指定目录下所有文件并显示在列表框中,希望对你有帮助 【【【推荐个牛逼得源码】】】 多线程同步问题,救命 如何用指定ip进行连接? 这种内存怎么释放,谢谢 请教。 ★★散100分+交友+电子书★★ 这个问题简单的我都不好意思提了,可我不会,关于html文件解析 请问什么时候才需要调用到“stdafx.h”?? 请问标志校验和空格校验在mscomm中如何用,含义如何? 能否动态调用函数 哪位大虾做过HTML或XML的解释器,能否提供一下思路 像flashGet,NetAnts中自动监视下载文件是怎么实现的??
{
CString dirall(pstr);
dirall+="\\*.*";
CFileFind finder;
BOOL bFind=(BOOL)finder.FindFile(dirall);
while(bFind)
{
bFind=finder.FindNextFile();
if(!finder.IsDots())
{
CString str = finder.GetFilePath();
if(finder.IsDirectory())
{
DeleteDir(str);
}
else
{
DeleteFile(str);
}
}
}
finder.Close();
if(!RemoveDirectory(pstr))
{
AfxMessageBox("删除文件目录出错!");
return FALSE;
}
return TRUE;
}
修改一下
CFileFind cf;
int nItem;
CString fName,fvideo,ftitle,fstime,fetime,fsize;
CFileStatus status;
CTime fctime,fmtime;
double filesize;_chdir(lzPath); //进入指定目录,lzPath是指定的路径。
b= cf.FindFile("*.*", 0); //执行通配符(*.*)的搜索,为了派生类的FindFile实现多态保留,后一个参数必须为0
strcpy(strPath,lzPath); while (b)
{
b=cf.FindNextFile(); //继续前次调用FindFile生成的文件搜索。如果有更多的文件,则返回非0值;如果是最后一个文件,并且先前FindFile或FindNextFile的调用都返回非0,则返回值为0 fName = cf.GetFileName(); //获得找到文件的文件名
ftitle = cf.GetFileTitle(); //得到指定文件的标题
CFile::GetStatus(fName,status);
fctime = status.m_ctime; //得到文件的创建时间
fstime.format("%.4d-%.2d-%.2d%.2d:%.2d:%.2d",fctime.GetYear(),fctime.GetMonth(),
fctime.GetDay(),fctime.GetHour(),fctime.GetMinute(),fctime.GetSecond());
fmtime = status.m_mtime; //得到文件最后修改时间
fetime.format((“.4d-%.2d-%.2d%.2d:%.2d:%.2d",fmtime.GetYear(),fmtime.GetMonth(), fmtime.GetDay(),fmtime.GetHour(),fmtime.GetMinute(),fmtime.GetSecond());
filesize =(double) (lzStatus.m_size)/1024; //文件大小
fsize.format("%.3lfKB",filesize);
//在列表视图中显示
nItem = m_list.InsertItem(count,fName);
m_list.SetItem(nItem,1,1,fvideo,NULL,0,0,0);
m_list.SetItem(nItem,2,1,fstime,NULL,0,0,0);
m_list.SetItem(nItem,3,1,fetime,NULL,0,0,0);
m_list.SetItem(nItem,4,1,fsize,NULL,0,0,0);
}
这是我做的一个程序,遍历指定目录下所有文件并显示在列表框中,希望对你有帮助