procedure SerachFile(const Path: string); //Path必须带“\”号
var
  StringList: TStringList;
  SearchRec: TSearchRec;
begin
  if FindFirst(Path + '*.*', faAnyFile, SearchRec) = 0 then
  begin
    StringList := TStringList.Create;
    repeat
      StringList.Add(SR.Name); 
    until FindNext(SR) <> 0;
    FindClose(SR);
    StringList.SaveToFile('c:\file.txt');
    StringList.Free;
  end;
end;

解决方案 »

  1.   

    編譯時提示:
    undeclared identifier :'SR'
    請問如何宣告這一變數?
      

  2.   

    procedure SerachFile(const Path: string); //Path必须带“\”号
    var
      StringList: TStringList;
      SR: TSearchRec;
    begin
      if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
      begin
        StringList := TStringList.Create;
        repeat
          StringList.Add(SR.Name); 
        until FindNext(SR) <> 0;
        FindClose(SR);
        StringList.SaveToFile('c:\file.txt');
        StringList.Free;
      end;
    end;
      

  3.   

    procedure SerachFile(const Path: string); //Path必须带“\”号
    var
      StringList: TStringList;
      SR: TSearchRec;
    begin
      if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
      begin
        StringList := TStringList.Create;
        repeat
          StringList.Add(SR.Name); 
        until FindNext(SR) <> 0;
        FindClose(SR);
        StringList.SaveToFile('c:\file.txt');
        StringList.Free;
      end;
    end;
      

  4.   

    執行後c:\file.txt顯示空白, 沒有東西,我的源碼如下請幫我看看有何問題,謝謝
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Form1: TForm1;
       StringList: TStringList;
      SR: TSearchRec;
       SearchRec: TSearchRec;
       path:string;
    begin
    path:='c:\windows\';  if FindFirst(Path + '*.*', faAnyFile, SearchRec) = 0 then
      begin
        StringList := TStringList.Create;
        repeat
          StringList.Add(SR.Name);
        until FindNext(SR) <> 0;
        FindClose(SR);
        StringList.SaveToFile('c:\file.txt');
        StringList.Free;
      end;
    end;
      

  5.   

    确实不错!
    procedure SerachFile(const Path: string); //Path必须带“\”号
    var
     StringList:TStringlist;
     SR:TSearchRec;
    begin
     if FindFirst(Path+'*.*',faAnyFile,SR)=0 then
     begin
      Stringlist:=TStringlist.Create;
      repeat
       Stringlist.add(sr.name);
      until findNext(SR)<>0;
       findClose(SR);
       StringList.SaveToFile('C:\file.txt');
       Stringlist.free
     end;
    end;
      

  6.   

    把if FindFirst(Path + '*.*', faAnyFile, SearchRec) = 0 then改为
    if FindFirst(Path + '*.*', faAnyFile, SR) = 0 就可以了。