频道分类

Delphi 根据扩展名的文件搜索

作者:admin 来源: 日期:2020/2/10 15:47:08 人气: 标签:

 
procedure GetAllFilesEM(Path, ExtMask: String; List: TStrings;
  SubFolder: Boolean);
var
 Attrib, k: Integer;
 Search: TSearchRec;
begin
 Attrib := faArchive + faReadOnly + faHidden;

 if Path[Length(Path)] <> '\' then Path := Path + '\';

 with TStringList.Create do
 try
   CommaText := ExtMask;

   for k := 0 to Count - 1 do
   if FindFirst(Path + '*.' + Strings[k], Attrib, Search) = 0 then
     repeat
       List.Add(AnsiUpperCase(Path + Search.Name));
     until FindNext(Search) <> 0;

   FindClose(Search);
 finally Free end;

 if SubFolder then
 begin
   if FindFirst(Path + '*.*', faDirectory, Search) = 0 then
   begin
     repeat
       if ((Search.Attr and faDirectory) = faDirectory) and
         (Search.Name[1] <> '.') then
         GetAllFilesEM(Path + Search.Name, ExtMask, List, SubFolder);
         Application.ProcessMessages ;
     until FindNext(Search) <> 0;

     FindClose(Search);
   end;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetAllFilesEM('d:\', '*.py', Memo1.Lines, True);
end;