在程序中怎么去搜索电脑中我指定的文件夹或文件.(这个文件或文件夹我事先并不知道)??????

hhzxf 2002-10-22 11:06:07
如题!!,答者有分
...全文
22 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
starcbh 2002-11-06
  • 打赏
  • 举报
回复
findfirst
findnext
Drate 2002-10-23
  • 打赏
  • 举报
回复
用下面的函数就可以解决你的问题了,只要在程序中动态指定你的目录就行了

// 从搜索记录中判断是否是子目录
function IsValidDir(SearchRec:TSearchRec):Boolean;
begin
if (SearchRec.Attr=16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..') then
Result:=True
else
Result:=False;
end;

// 子目录级的文件查询--查询主体函数
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;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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