不知道有人调用过_GetFileList(AStrings: TStrings; ASourFile, FileName: string);函数没。我是在网上找到的代码。如下:
procedure TfrmExport._GetFileList(AStrings: TStrings; ASourFile, FileName: string);
var sour_path, sour_file: string;
  TmpList: TStringList;
  FileRec, subFileRec: TSearchrec;
  i: Integer;
begin
  if RightStr(trim(ASourFile), 1) <> '\' then
    sour_path := trim(ASourFile) + '\'
  else
    sour_path := trim(ASourFile);
  sour_file := FileName;  if not DirectoryExists(sour_path) then
  begin
    AStrings.Clear;
    exit;
  end;  TmpList := TStringList.Create;
  TmpList.Clear;  if FindFirst(sour_path + '*.*', faAnyfile, FileRec) = 0 then
    repeat
      if ((FileRec.Attr and faDirectory) <> 0) then
      begin
        if ((FileRec.Name <> '.') and (FileRec.Name <> '..')) then
          _GetFileList(AStrings, sour_path + FileRec.Name + '\', sour_file);
      end
      else
        if FindFirst(sour_path + FileName, faAnyfile, subFileRec) = 0 then
          repeat
            if ((subFileRec.Attr and faDirectory) = 0) then
              TmpList.Add(sour_path + subFileRec.Name);
          until FindNext(subFileRec) <> 0;
    until FindNext(FileRec) <> 0;  SysUtils.FindClose(FileRec);
  for i := 0 to TmpList.Count - 1 do
    AStrings.Add(TmpList.Strings[i]);
  TmpList.Free;
end;不知道为什么,出来的结果有问题,重复循环。某文件夹下,有几个文件,就循环几次。。
不知道该怎么去改。。麻烦高手帮帮忙哦。。还有,顺便问下((FileRec.Name <> '.') and (FileRec.Name <> '..')) 是什么意思。

解决方案 »

  1.   

    在win3.1有这种控件的。
    另外给你一个调试成功的例子
    如何抓取一個目錄下的所有(很多)文件的名稱?procedure TForm1.Button1Click(Sender: TObject);
    var ss:Tsearchrec;
    filepath:string;
    begin
       filepath:='c:\';
       listbox1.Items.Clear;
       if findfirst(filepath+'*.*',faAnyFile,ss)=0 then
       begin
       if not ((ss.Attr and fadirectory)=fadirectory) then
       listbox1.Items.Add(ss.Name);
       while findnext(ss)=0 do
       begin
       if not ((ss.attr and fadirectory)=fadirectory) then
       listbox1.Items.Add(ss.Name);
       end;
       findclose(ss);
       end;
    end; 回复人: terrytzq(边缘) ( ) 信誉:95  2005-1-13 16:31:52  得分: 5  
     
     
       
    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;
      searchfl,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 FIndfirst(mainpath+filename,faAnyFile,searchfl)=0 then begin
            found:=true;
            foundresult.Add(mainpath+searchfl.Name);
            while (FindNext(searchfl)=0) do begin
              foundresult.Add(mainpath+searchfl.Name);
            end;
          end;
          FindCLose(searchfl);
          for i:=0 to subdir1.Count-1 do
            found:=Searchfile(mainpath+subdir1.Strings[i]+'\',Filename,foundresult)or found;
          subdir1.Free;
        end;
        result:=found;
      End;
    filename:=*.*;  
     
      

  2.   

    啊。谢谢谢谢哦。我一会试试。
    麻烦问下 SearchRec.Name<>'.' 是什么意思啊?
      

  3.   

    麻烦再问下哦。。function SearchFile(mainpath:string; filename:string;
             var foundresult:TStrings):Boolean;
    怎么给参数啊?如果我是找 f:\test\ 文件夹下所有的 *.xls 文件。。该怎么穿参数。。
    var foundresult:TStrings 是什么意思哦。
    我的东西想显示在 listbox里面麻烦大家再给我说说吧。。急用急用