如何得到一个目录下所有文件的文件名

解决方案 »

  1.   

    //获取指定目录下的文件列表,支持通配符。
        procedure GetDirFileList(aTypeName, aDirName: String; var ReturnStringList:
                TStringList; isAndDirectory: Boolean);-------------//获取指定目录下的文件名称
    procedure TDirectory.GetDirFileList(aTypeName, aDirName: String; var
            ReturnStringList: TStringList; isAndDirectory: Boolean);
    {  返回值为字符串列表  }
    var
      sr: TSearchRec;
      FileAttrs: Integer;
    begin
      ReturnStringList.Clear;
      aDirName := DecorateDirPath(aDirName);
      if isAndDirectory then
        FileAttrs := faDirectory
      else
        FileAttrs := faAnyFile;
      if FindFirst(aDirName + aTypeName, FileAttrs, sr) = 0 then
      begin
        repeat
          if (sr.Attr and FileAttrs) = sr.Attr then
            ReturnStringList.Add(sr.name);
        until FindNext(sr) <> 0;
        FindClose(sr);
      end;
    end; { GetDirFile }
      

  2.   

    和 CDSoftwareWj(95927) 兄 竞争一下
    procedure SearchCurrentFiles(const Path,FileType:string; Strings: TStrings);
    var
      Search:TSearchRec;
      iCount:integer;
      FilePath:string;
    begin
      FilePath:=Path;
      if Path[Length(Path)]<>'\' then
      FilePath:=Path+'\';  Try
        iCount:=FindFirst(FilePath +'*.*',faAnyFile,Search);
        while iCount =0 do
        begin
          if (Search.name<>'.')and(Search.name<>'..') then
          begin
            //if ExtractFileExt(Search.Name)=trim(FileType) then
              Strings.Add(Search.Name);
          end;
          iCount:=FindNext(Search)
        end;
      finally
        FindClose(Search);
      end;end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Path,FileType:string;
    begin
      Path:='F:\mp3\';
      //FileType:='mp3' ;
      SearchCurrentFiles(Path,FileType,ListBox1.Items);
    end;
      

  3.   

    这是我写的一个控件的一部分代码,可以完成你想要的功能使用时要修改一下这里procedure TDirectory.GetDirFileList
              ^^^^^^^^^^例子:
    var
      tempStrList: TStringList;
    begin
      .....
      //Create tempStrList
      .....
      GetDirFileList('*.pas', 'c:\temp', tempStrList, False); // False - 文件 True - 目录
      .....
      // tempStrList中就是文件列表了
    end;下面是
    function TDirectory.DecorateDirPath(aDirPath: String): string;
    begin
      if aDirPath[Length(aDirPath)]<> '\' then
        Result := aDirPath+'\'
      else
        Result := aDirPath;
    end;
    功能不用说了吧
      

  4.   

    哈哈 cll007(gazo) 兄弟的功能没有我的强 :)你的是我的最初版本....  hehe^^ 偏一下
      

  5.   

    我的编程经验比较少,觉得cll007(gazo)的程序比较好理解一些
      

  6.   

    不过CDSoftwareWj(95927) 的功能比较强大一些,谢谢!