如何获得指定目录下,指定后缀名的所有文件的文件名?
如何循环打开这些文件?
如何随机打开这些文件?

解决方案 »

  1.   

    //列出所有指定目录下的txt文件
    function   ListFiles(Dir: String):TStrings;
    var FSearchRec:   TSearchRec;
        FileList:     TStrings;
        FindResult:   Integer;
    begin
       if Dir[length(Dir)]<>'\'  then  Dir:=Dir+'\';
          FileList  :=TStringList.Create;
          FindResult:=FindFirst(Dir+'*.txt',faAnyFile+faDirectory,FSearchRec);
           try
             while FindResult = 0  do
                begin
                FileList.Add(LowerCase(Dir+FSearchRec.Name));
                FindResult:=FindNext(FSearchRec);
                end;
           finally
            FindClose(FSearchRec);
           end;
          //FileList.Sorted:=true;
          ListFiles:=FileList;
    end;
      

  2.   

    把查到的文件放到一个列表中(TStringlist),然后随机访问索引得到要打开的文件名打开
      

  3.   


    TStringlist有极限吧?要考虑文件非常多的时情况。
      

  4.   

    能读取所有下级文件夹里所有指定后缀文件的函数。
    function FindFile(Path: string): string; //path是搜索的穆鲁
    var
     Sr: TSearchRec;
      CommaList: TStringList;
      s: string;
      dt: TDateTime;
    begin
      commalist := Tstringlist.Create;
      try
        Findfirst(path + '*.*', faAnyFile, sr);//*.*表示所有文件可以自己指定
        if ((Sr.Attr and faDirectory) > 0) and (Sr.Name <> '.') then
        begin
          dt := FileDateToDateTime(sr.Time);
          s := FormatDateTime('yyyy-mm-dd hh:nn', dt);
          commalist.add('*' + s + sr.name);
        end;
        while findnext(sr) = 0 do
        begin
          if ((Sr.Attr and faDirectory) > 0) and (Sr.Name <> '..') then
          begin
            dt := FileDateToDateTime(sr.Time);
            s := FormatDateTime('yyyy-mm-dd hh:nn', dt);
            commalist.add('*' + s + sr.name);
          end;
        end;
        FindClose(sr);
        FindFirst(path + '*.*', faArchive + faReadOnly + faHidden + faSysFile, Sr);
        if Sr.Attr <> faDirectory then
        begin
          dt := FileDateToDateTime(sr.Time);
          s := FormatDateTime('yyyy-mm-dd hh:nn', dt);
          commalist.add('\' + s+ Format('%.0n', [sr.Size / 1]) + '|' + sr.name);
        end; //Inttostr(
        while findnext(sr) = 0 do
        begin
          if (sr.Attr <> faDirectory) then
          begin
            dt := FileDateToDateTime(sr.Time);
            s := FormatDateTime('yyyy-mm-dd hh:nn', dt);
            commalist.add('\' + s +Format('%.0n', [sr.Size / 1]) + '|' + sr.name);
          end;
        end;
        FindClose(Sr);
      except
      end;
      Result := commalist.Text;     //Result是消息的 定义
      commalist.Free;
    end;
      

  5.   

    有个FindFile控件很不错,试试看