如何得到某个目录下的所有文件的文件名呢?小弟初学D,望指教!
想得到所有的文件名,基本上都是些图片,然后自动设置为桌面
写着玩的,谢谢
第一次提问,分就100吧

解决方案 »

  1.   

    FileListBox
    ShellListBox
    都是Delphi6/7自带的第三方的NameSpace,ShellControl,LsFileExplorer都是很好的
      

  2.   

    给你一个函数吧。
    function SearchFile( RootPath, FileName: String; var lstFile: TStrings ):Boolean;//---------------------------------------------------------------------------
    //
    //      查找一个目录下的文件,写在lstFile
    //
    //---------------------------------------------------------------------------
    function SearchFile( RootPath, FileName :String; var lstFile: TStrings ):Boolean;
    var
      FileRec     : TSearchRec;
    begin
      if RootPath[Length(RootPath)] <>'\' then
         RootPath := RootPath +'\';  if ( FindFirst( RootPath + FileName, faAnyFile, FileRec ) =0 ) then
      begin
        lstFile.Add( RootPath + FileRec.Name );
        while ( FindNext( FileRec ) = 0 ) do
        begin
          lstFile.Add( RootPath + FileRec.Name );
        end;
      end;
      FindClose( FileRec );  Result := True;
    end;使用方法:
    var
      lstFiles :TString;
    begin
      lstFiles := TStringList.Create;
      SearchFile( 'C:\', *.*, lstFiles );
      
      // c盘下的所有文件名称都在lstFiles中了。
      // 文件名一就是:lstFiles.Strings[0];
      // 文件名二就是:lstFiles.Strings[1];
    end;
      

  3.   

    文件遍历:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, FileCtrl, ComCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        PageControl1: TPageControl;
        TabSheet1: TTabSheet;
        Panel1: TPanel;
        ListView1: TListView;
        Panel2: TPanel;
        Button2: TButton;
        Edit1: TEdit;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure IniListView();
        procedure ListView1Click(Sender: TObject);
        procedure FormShow(Sender: TObject);
        procedure ListView1DblClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      function GetDirectoryName(Dir:String):String;  //设置DIR
      procedure FindFiles(APath:String);    //查找主函数  end;var
      Form1: TForm1;
      Dir1:string;
      pathpos:integer;
      selectpath:string;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      //winexec('rundll32.exe shell32.dll,Control_RunDLL Intl.cpl,,0',9);
      close;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
       SelectDirectory('Select Directory', '', Dir1);
       if Dir1<>'' then
         begin
           edit1.Text:=Dir1;
           ListView1.Items.Clear;
           FindFiles(Dir1);
         end;
    end;procedure TForm1.IniListView();
    var
      NewColumn: TListColumn;
    begin
      with ListView1 do
      begin
        Parent := panel1;
        Align := alClient;
        OnClick := ListView1Click;  // <- 加上这句    ViewStyle := vsReport;    NewColumn := Columns.Add;
        NewColumn.Caption := '素材名称';
        NewColumn.Width:=100;
        NewColumn := Columns.Add;
        NewColumn.Caption := '文件类型';
        NewColumn.Width:=100;
        NewColumn := Columns.Add;
        NewColumn.Caption := '路径';
        NewColumn.Width:=200;
        NewColumn := Columns.Add;
        NewColumn.Caption := '文件大小';
        NewColumn.Width:=100;
        NewColumn := Columns.Add;
        NewColumn.Caption := '更新时间';
        NewColumn.Width:=100;  end;
    end;function Tform1.GetDirectoryName(Dir:String):String;  //设置DIR
    begin
      if Dir[LengTh(Dir)]<>'\' then
        Result:=Dir+'\'
      else
        Result:=Dir;
    end;procedure Tform1.FindFiles(APath:String);    //查找主函数
    var
      FSearchRec,DsearchRec:TSearchRec;
      FindResult:integer;
      it:tlistitem;
      fhandle:thandle;
      hip:pointer;
      filesize:longint;
      /////////
      tempstr:string;
    function IsDirNotation(ADirName:String):Boolean;
    begin
      Result:=(ADirName='.') or(ADirName='..')or(ADirName='') ;
    End;begin
     Apath:=GetDirectoryName(APath);
     FIndResult:=FindFirst(APath+'*.*',faAnyFile+faHidden+faSysFile+FaReadonly,FsearchRec);
     Try
     while FindResult=0 do
     begin
     it:=listview1.Items.Add;
     it.Caption:=extractfilename(APath+FSearchRec.Name);    //把查找到的文件列出来
     it.SubItems.Add(extractfileext(APath+FSearchRec.Name));//列出文件的文件类型
     it.SubItems.Add(APath+FSearchRec.Name); fhandle:=createfile(pchar(APath+FSearchRec.Name),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
     new(hip);
     filesize:=GetFileSize(fhandle,hip);
     dispose(hip);
     it.SubItems.Add(FormatFloat('#,000',filesize)+'   字节');
     //获得所选路径以下文件路径
     //tempint:=pos(nowdir,copy(apath,pathpos,length(apath)-pathpos+1));
     //tempstr:=copy(apath,tempint+pathpos,length(apath)-tempint-pathpos);
     tempstr:=copy(apath,pathpos,length(apath)-pathpos+1);
     it.SubItems.Add(selectpath+tempstr+it.Caption);
     closehandle(fhandle);
     FindResult:=FindNext(FSearchRec);
     end;
     FindReSult:=FindFirst(APath+'*.*',faDirectory,DSearchRec);
     while FindResult=0 do
     begin
     if (DSearchRec.Attr{ and faDirectory)}=faDirectory) and not ISDirNotation(DSEarchRec.Name) then
     FindFiles(APath+DsearchRec.Name);
     FindResult:=FindNext(DsearchREc);
     end;
     Finally
      FindClose(FSearchRec);
      findclose(DsearchREc);
     end;
    end;procedure TForm1.ListView1Click(Sender: TObject);
    begin
      //hjfgh
    end;procedure TForm1.FormShow(Sender: TObject);
    begin
           IniListView;
           edit1.Text:=extractfilepath(Application.ExeName);
           FindFiles(extractfilepath(Application.ExeName));
    end;procedure TForm1.ListView1DblClick(Sender: TObject);
    var
      i:integer;
    begin
          if TListView(Sender).ItemIndex>=0 then  // 把ListView1改为TListView(Sender)
            begin
              i:=TListView(Sender).ItemFocused.Index;
              showmessage(ListView1.Items[i].SubItems[1])
            end
    end;end.
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
       SearchRec:TSearchRec;
       ext,AppPath:string;
       dot,dotdot:TFileName;
    begin
        listbox1.Items.Clear;
         AppPath:='d:\test\';
         ext:='*.*';
         FindFirst(AppPath+ext,faAnyFile-faDirectory,SearchRec);
         listbox1.Items.Add(SearchRec.Name);
         while FindNext(SearchRec)=0 do
           listbox1.Items.Add(SearchRec.Name);
         listbox1.items为你需要的结果
    end;
      

  5.   

    在各位的帮助之下,我已经把我的问题解决了。真是没有想到,我的一个问题,从提问到结束,只有
    不到一天的时间,得到圆满的解决。
    在此特别感谢各位朋友!尤其是
    dickeybird888(于伟刚) 
    2312(007) 以及flyinwuhan(超越) 
    等,谢谢