procedure TForm1.FindExe(sDirectory: String;sl:TStringList);
var
sr: TSearchRec;
sPath,sExt,sFile: String;
begin
//检查目录名后面是否有 '\'
if Copy(sDirectory,Length(sDirectory),1) <> '\' then
sPath := sDirectory + '\'
else
sPath := sDirectory;
//------------------------------------------------------------------
if FindFirst(sPath+'*.*',faAnyFile, sr) = 0 then
begin
repeat
sFile:=Trim(sr.Name);
if sFile='.' then Continue;
if sFile='..' then Continue;
sFile:=sPath+sr.Name;
if (sr.Attr and faDirectory)<>0 then
FindExe(sFile,sl)
else if (sr.Attr and faAnyFile) = sr.Attr then
begin
sExt:=UpperCase(ExtractFileExt(sFile));
if sExt='.EXE' then
sl.Add(sFile);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
//------------------------------------------------------------------
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sl:TStringList;
begin
sl:=TStringList.Create;
try
FindExe('D:\Program',sl);
sl.SaveToFile('C:\Result.txt');
finally
sl.Free;
end;
end;