请问怎么写一个搜索某一类文件的函数啊?

sea_way 2003-08-22 11:29:25
请问怎么写一个搜索某一类文件的函数啊?
我想写一个磁盘垃圾清理程序,要先写一个返回指定目录下指定类型文件路径的函数.
谁能个小例子啊.相关的也可以.谢谢了.
...全文
45 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sea_way 2003-08-28
  • 打赏
  • 举报
回复
帮忙支持一下啊.各位.
sea_way 2003-08-26
  • 打赏
  • 举报
回复
这几个例子都不怎么合适啊.请大侠们帮帮小弟啊.谢谢
huojiehai 2003-08-23
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1656/1656068.xml?temp=.3142206
里面有文件删除的函数,用的是递归,你改一下,应该能满足你的要求
sea_way 2003-08-23
  • 打赏
  • 举报
回复
好的.我试试.看谁的好用
Dhammacakkhu 2003-08-23
  • 打赏
  • 举报
回复
这是一个取得指定目录下所以ACCESS文件的例子:
procedure TRestore.GetAccessFile;
var
sr: TSearchRec;
fr: Integer;
begin
fr := FindFirst(ShellTreeview1.Path + '\*.MDB',faAnyFile,sr);
if fr=0 then
repeat
if (sr.Attr <> faDirectory) then
begin
ListBox1.Items.Add(sr.Name);
end;
until SysUtils.FindNext(sr) <> 0;
FindClose(sr);
end;
mj_hc 2003-08-23
  • 打赏
  • 举报
回复
//这是在一个目录下查找文件的列子,你只要改一下就可。
function IsValidDir(SearchRec:TSearchRec):Boolean;
begin
if (SearchRec.Attr=16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..') then
Result:=True
else
Result:=False;
end;
2. 这是查询主体函数。
参数介绍:

Mainpath: 指定的查询目录。
Filename: 欲查询的文件。
Foundresult: 返回的含完整路径的匹配文件(可能有多个)。

如果有匹配文件,函数返回True,否则,返回False;  

function SearchFile(mainpath:string; filename:string;
var foundresult:TStrings):Boolean;
var i:integer;
Found:Boolean;
subdir1:TStrings;
searchRec:TsearchRec;
begin
found:=false;
if Trim(filename)<>'' then
begin
subdir1:=TStringList.Create;//字符串列表必须动态生成
//找出所有下级子目录。
if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);
//查找当前目录。
if FileExists(mainpath+filename) then
begin
found:=true;
foundresult.Add(mainpath+filename);
end;
//这是递归部分,查找各子目录。
for i:=0 to subdir1.Count-1 do
found:=Searchfile(mainpath+subdir1.Strings[i]+
'\',Filename,foundresult)or found;
//资源释放并返回结果。
subdir1.Free;
end;
result:=found;
end;
lw549 2003-08-23
  • 打赏
  • 举报
回复
findfirst
findnext

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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