***只给定一个本地盘(如c或d或e),如何取得符合文件后缀的所有文件***

firephoenix001 2005-03-07 06:12:37
比如如何取得后缀为.txt的在该盘下的所有文件??
小妹求助
...全文
140 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangming120 2005-03-08
  • 打赏
  • 举报
回复
//说明:
//TFindCallBack为回调函数,FindFile函数找到一个匹配的文件之后就会调用这个函数。
//TFindCallBack的第一个参数找到的文件名,你在回调函数中可以根据文件名进行操作。
//TFindCallBack的第二个参数为找到的文件的记录信息,是一个TSearchRec结构。
//TFindCallBack的第三、四个参数分别为决定是否终止文件的查找,临时决定是否查找某个子目录!
//FindFile的参数:
//第一个决定是否退出查找,应该初始化为false;
//第二个为要查找路径;
//第三个为文件名,可以包含Windows所支持的任何通配符的格式;默认所有的文件
//第四个为回调函数,默认为空
//第五个决定是否查找子目录,默认为查找子目录
//第六个决定是否在查找文件的时候处理其他的消息,默认为处理其他的消息
//若有意见和建议请E_Mail:Kingron@163.net
type
TFindCallBack=procedure (const filename:string;const info:TSearchRec;var bQuit,bSub:boolean);


procedure FindFile(var quit:boolean;const path: String;const filename:string='*.*';
proc:TFindCallBack=nil;bSub:boolean=true;const bMsg:boolean=true);
var
fpath: String;
info: TsearchRec;

procedure ProcessAFile;
begin
if (info.Name<>'.') and (info.Name<>'..') and ((info.Attr and faDirectory)<>faDirectory) then
begin
if assigned(proc) then
proc(fpath+info.FindData.cFileName,info,quit,bsub);
end;
end;

procedure ProcessADirectory;
begin
if (info.Name<>'.') and (info.Name<>'..') and ((info.attr and fadirectory)=fadirectory) then
findfile(quit,fpath+info.Name,filename,proc,bsub,bmsg);
end;

begin
if path[length(path)]<>'\' then
fpath:=path+'\'
else
fpath:=path;
try
if 0=findfirst(fpath+filename,faanyfile and (not fadirectory),info) then
begin
ProcessAFile;
while 0=findnext(info) do
begin
ProcessAFile;
if bmsg then application.ProcessMessages;
if quit then
begin
findclose(info);
exit;
end;
end;
end;
finally
findclose(info);
end;
try
if bsub and (0=findfirst(fpath+'*',faanyfile,info)) then
begin
ProcessADirectory;
while findnext(info)=0 do
ProcessADirectory;
end;
finally
findclose(info);
end;
end;
例子:
procedure aaa(const filename:string;const info:tsearchrec;var quit,bsub:boolean);
begin
form1.listbox1.Items.Add(filename);
quit:=form1.qqq;//qqq为form1的自定义boolean变量
bsub:=form1.checkbox1.Checked;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.Clear;
qqq:=false;
button1.Enabled:=false;
findfile(qqq,edit1.text,edit2.text,aaa,checkbox1.checked,checkbox2.checked);
showmessage(inttostr(listbox1.items.count));
button1.Enabled:=true;
end;
你稍作修改应该没问题,祝你节日快乐!!!!嗬嗬
firephoenix001 2005-03-08
  • 打赏
  • 举报
回复
sign!!!
firephoenix001 2005-03-08
  • 打赏
  • 举报
回复
也许我没说明白,比如d盘下还有好多“文件夹“,这些“文件夹“下还有 “文件夹“,在这些“文件夹“下还可能有。。。,我要的是只给定d就可以得到我要的“文件“。
firephoenix001 2005-03-08
  • 打赏
  • 举报
回复
不可以,我设了个buttonclick时间调用该函数,FindFirst(FPath+'*.txt', FileAttrs, sr)=2直接跳出了!!我困惑!!
herman~~ 2005-03-07
  • 打赏
  • 举报
回复
回复人: belllab(菜鸟) (
来晚了,已经解决
belllab 2005-03-07
  • 打赏
  • 举报
回复
能不用第三方的时候我是尽量不用,不是要写很多代码才能完成的时候我也是不会用第三方控件的,呵呵。
顺便也是锻炼自己吧。
科技互联人生 2005-03-07
  • 打赏
  • 举报
回复
procedure FindAllFile(FPath:String);
var sr: TSearchRec;
FileAttrs: Integer;
begin
FileAttrs := faReadOnly + faHidden+ faSysFile+ faVolumeID+ faDirectory+faArchive+faAnyFile;
if FindFirst(FPath+'*.txt', FileAttrs, sr) = 0 then begin
repeat
//是目录
if ((sr.Attr and faDirectory) = faDirectory)
and (sr.Name<>'.') and (sr.Name<>'..') then begin
FindAllFile(FPath+sr.Name+'\');
end else begin
//是文件
slTemp.Add(FPath+sr.Name);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
是可以实现的,学习...


我平时也借助一些第三方的控件来实现,比如:Disk.Controls
这个来找文件是很方便的,绑定几个属性(查找选项)就OK 了
firephoenix001 2005-03-07
  • 打赏
  • 举报
回复
谢谢,我今晚上看看
belllab 2005-03-07
  • 打赏
  • 举报
回复
//刚好正在开发的程序有一个类似的函数,自己改造一下吧。用了递归。
//定义一个外部变量,slTemp(TStringList)用来保存找到的文件信息。

procedure FindAllFile(FPath:String);
var sr: TSearchRec;
FileAttrs: Integer;
begin
FileAttrs := faReadOnly + faHidden+ faSysFile+ faVolumeID+ faDirectory+faArchive+faAnyFile;
if FindFirst(FPath+'*.txt', FileAttrs, sr) = 0 then begin
repeat
//是目录
if ((sr.Attr and faDirectory) = faDirectory)
and (sr.Name<>'.') and (sr.Name<>'..') then begin
FindAllFile(FPath+sr.Name+'\');
end else begin
//是文件
slTemp.Add(FPath+sr.Name);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
firephoenix001 2005-03-07
  • 打赏
  • 举报
回复
急死了!!!!!!!!!!!!!!!
chinaandys 2005-03-07
  • 打赏
  • 举报
回复
up

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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