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;这个函数我是搜索文件使用,但是要是输入的是*.exe的话返回值就变成了 比如: c:\winnt\*.exe 如果让返回的结果变成确实的文件名 如c:\winnt\aa.exe ,请教高手,谢谢
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;这个函数我是搜索文件使用,但是要是输入的是*.exe的话返回值就变成了 比如: c:\winnt\*.exe 如果让返回的结果变成确实的文件名 如c:\winnt\aa.exe ,请教高手,谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货