怎样遍历目录并对其中文件操作

_xiaolifeidao 2001-12-22 01:23:39
我要写一个遍历目录的算法,各位大侠帮忙啊:)
...全文
138 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
_xiaolifeidao 2001-12-25
  • 打赏
  • 举报
回复
哪位大侠能不能发一个完整的源码到我信箱吗?
wang_xuping@263.net
iconlee 2001-12-24
  • 打赏
  • 举报
回复
收藏
zjhsoft 2001-12-24
  • 打赏
  • 举报
回复
可以用CFileFind类的FindFile()及FindNextFile()来实现.
或者
用FindFirstFile() and FindNextFile()来实现
eng 2001-12-24
  • 打赏
  • 举报
回复
不错不错!
darkbb 2001-12-24
  • 打赏
  • 举报
回复
楼上的兄弟讲得真全
xtky_limi 2001-12-24
  • 打赏
  • 举报
回复
可以用递归实现
FindDir( dir , ....)
{
利用CFileFind
找到符合条件的项
while
{
找到下一个符合条件项
if( 此项为目录 )
{
FindDir ( 此项 ,。。。)
}
else
{
符合条件的文件:
进行处理
}
}
}
ccnuxjg 2001-12-24
  • 打赏
  • 举报
回复
调用方法:
DelDirectory("D:\\Temp"
ygd 2001-12-24
  • 打赏
  • 举报
回复

void CFindFileDlg::Find()
{
long handle;
struct _finddata_t filestruct;
char path_search[256];
handle=_findfirst( "*",&filestruct);
if(handle==-1)return;
if(::GetFileAttributes(filestruct.name)&FILE_ATTRIBUTE_DIRECTORY)
{
if(filestruct.name[0]!='.')
{_chdir(filestruct.name);
Find();
_chdir("..");
}

}
else
{
_getcwd(path_search,256);
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
length=GetLines(path_search);
strcat(path_search,"******* ");
itoa(length,buf,10);
strcat(path_search,buf);
strcat(path_search,"\n");
fwrite(path_search,strlen(path_search),1,fp);
}
while(!_findnext(handle,&filestruct))
{
if(::GetFileAttributes(filestruct.name)&FILE_ATTRIBUTE_DIRECTORY)
{
if(filestruct.name[0]!='.')
{_chdir(filestruct.name);
Find();
_chdir("..");
}
}
else
{
_getcwd(path_search,256);
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
length=GetLines(path_search);
strcat(path_search,"********* ");
itoa(length,buf,10);
strcat(path_search,buf);
strcat(path_search,"\n");
fwrite(path_search,strlen(path_search),1,fp);
}
}
}
_xiaolifeidao 2001-12-24
  • 打赏
  • 举报
回复
谢谢ccnuxjg(阿木)
不过我试过了
程序运行之后就跳到程序的当前目录下面去了
请问这是怎么回事呢?
shipatrioc 2001-12-22
  • 打赏
  • 举报
回复
g z g z
ccnuxjg 2001-12-22
  • 打赏
  • 举报
回复
BOOL CCutfoldDlg::DelDirectory(CString DirName)
{
CFileFind tempFind;
char tempFileFind[200];
sprintf(tempFileFind,"%s\\*.*",DirName);
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile();
if(!tempFind.IsDots())
{
char foundFileName[200];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
if(tempFind.IsDirectory())
{
char tempDir[200];
sprintf(tempDir,"%s\\%s",DirName,foundFileName);
DelDirectory(tempDir);
}
else
{
char tempFileName[200];
sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
DeleteFile(tempFileName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(DirName))
{
::MessageBox(0,"删除目录失败!","警告信息",MB_OK);
return FALSE;
}
return TRUE;
}
sgr0426 2001-12-22
  • 打赏
  • 举报
回复
CFileFind类 的方法
seamore 2001-12-22
  • 打赏
  • 举报
回复
可以用CFileFind类的FindFile()及FindNextFile()来实现.
wwwsq 2001-12-22
  • 打赏
  • 举报
回复
用CFileFind

16,550

社区成员

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

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

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