如何取得一个文件夹里的所有文件的文件名?
谢谢!

解决方案 »

  1.   

    unit FileSerach;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, ExtCtrls;type
      Tfilefindfrm = class(TForm)
        lbfiles: TListBox;
        Panel1: TPanel;
        Button1: TButton;
        Edit2: TEdit;//目录
        Edit1: TEdit;//文件名如*.Txt等
        Label1: TLabel;
        Label2: TLabel;
        procedure Button1Click(Sender: TObject);  private
        { Private declarations }
      public
      FFileName:String;  //查找的文件名
      function GetDirectoryName(Dir:String):String;
      Procedure FindFiles(APath:String);
        { Public declarations }
      end;var
      filefindfrm: Tfilefindfrm;implementation{$R *.dfm}
    function Tfilefindfrm.GetDirectoryName(Dir:String):String;  //设置DIR
    begin
     if Dir[LengTh(Dir)]<>'\' then
      Result:=Dir+'\'
     else
     Result:=Dir;
    end;procedure TfilefindFrm.FindFiles(APath:String);    //查找主函数
    var
    FSearchRec,DsearchRec:TSearchRec;
    FindResult:integer;function IsDirNotation(ADirName:String):Boolean;
    begin
    Result:=(ADirName='.') or(ADirName='..');
    End;begin
     Apath:=GetDirectoryName(APath);
     FIndResult:=FindFirst(APath+FFileName,faAnyFile+faHidden+faSysFile+FaReadonly,FsearchRec);
     Try
     while FindResult=0 do
     begin
     lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name));    //把查找到的文件列出来
     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);
      end;
      end;procedure Tfilefindfrm.Button1Click(Sender: TObject);
    begin
      Screen.Cursor:=CrHourGlass;
      try
      lbFiles.Items.Clear;
      FFIleName:=Edit2.Text;
      FindFiles(Edit1.Text);
      Finally
      Screen.Cursor:=crDefault;
    end;
    end;end.
      

  2.   

    实际上就是调用FindFirst,FindNext这些函数.
      

  3.   

    给你一个例子:
    var
      Flist:Tstringlist;
      i:integer;
    begin
      FList:= TStringList.Create;
      SearchFile('C:\abc'+'\*.*', FList, faDirectory, True);
      FileLV.items.BeginUpdate;
      FileLV.items.clear;
      For I:=0 To FList.Count -1 Do
        With FileLV.Items.Add Do
          Caption:= FLIst.Strings[I];
      FileLV.items.EndUpdate;    
      FList.Free;
    End;FileLV 是一个LISTVIEW控件.函数来了:
    unit CusTomFerFuns;Interface
    uses
      Forms,ComCtrls,SysUtils,Windows,ADODB,Mask,stdctrls,Dialogs,Classes; Procedure SearchDirectory(Directory: String; DList: TStringList;
                              ExcludeAttr: Integer; InDir: Boolean);
     Procedure SearchFile(FileName: TFileName; FList: TStringList;
                        ExcludeAttr: Integer; InDir: Boolean);
    implementationProcedure CustomSearchFile(SearchPath: String; List: TStringList;
                               ExcludeAttr: Integer; InDir: Boolean);
    Var
      Index: Integer;
      NextDir: Boolean;
      FindData: TWin32FindData;
      FindHandle: LongWord;
      FileName: TFileName;
    Begin
      IF Copy(SearchPath, Length(SearchPath), 1)<>'\' Then SearchPath:= SearchPath+ '\';
      FindHandle:= FindFirstFile(PChar(SearchPath+ '*.*'), FindData);
      NextDir:= False;
      Index:= List.Count;
      While True Do Begin
        IF NextDir And InDir Then Begin
          FindHandle:=INVALID_HANDLE_VALUE;
          While Index<List.Count Do Begin
            IF Not FileExists(List.Strings[Index]) Then Inc(Index)
            Else
            Begin
              SearchPath:= List.Strings[Index]+'\';
              FindHandle:= FindFirstFile(pChar(SearchPath+ '*.*'), FindData);
              Break;
            End;
          End;
        End;
        IF FindHandle= INVALID_HANDLE_VALUE Then Break;    FileName:= FindData.cFileName;
        IF (FindData.dwFileAttributes And ExcludeAttr)=0 Then
          IF FileName<>'.' Then
            IF FileName<>'..' Then
              List.Add(SearchPath+ FileName);    IF Not FindNextFile(FindHandle, FindData) Then Begin
          windows.FindClose(FindHandle);
          IF InDir Then NextDir:= True Else Break;
        End;
      End;
    End;Procedure SearchDirectory(Directory: String; DList: TStringList;
                              ExcludeAttr: Integer; InDir: Boolean);
    Var
      Index: Integer;
    Begin
      IF ExcludeAttr=-1 Then ExcludeAttr:= faSysFile OR faHidden;
      IF (ExcludeAttr And faDirectory)= faDirectory Then
         ExcludeAttr:= ExcludeAttr Xor faDirectory;
      Index:= DList.Count;
      CustomSearchFile(Directory, DList, ExcludeAttr, InDir);
      For Index:=DList.Count-1 DownTo Index Do
        IF FileExists(DList.Strings[Index]) Then DList.Delete(Index);
    End;Procedure SearchFile(FileName: TFileName; FList: TStringList;
                        ExcludeAttr: Integer; InDir: Boolean);
    Var
      SPath, SFile: String;
      Index, BufferIndex: Integer;
      FindData: TWin32FindData;
      FindHandle: LongWord;
    Begin
      SPath:= ExtractFilePath(FileName);
      SFile:= ExtractFileName(FileName);
      Index:= FList.Count;
      BufferIndex:= Index;
      IF ExcludeAttr=-1 Then ExcludeAttr:= faDirectory OR faSysFile OR faHidden;  While True Do Begin
        IF InDir Then SearchDirectory(SPath, FList, ExcludeAttr, False);
        FindHandle:= FindFirstFile(PChar(SPath+SFile), FindData);
        While FindHandle<>INVALID_HANDLE_VALUE  Do Begin
          FileName:= FindData.cFileName;
          IF (ExcludeAttr=0) OR ((FindData.dwFileAttributes And ExcludeAttr)=0) Then
            IF FileExists(SPath+FileName) Then FList.Add(SPath+FileName);      IF Not FindNextFile(FindHandle, FindData) Then Begin
             FindClose(FindHandle);
             Break;
          End;
        End;
    //
        IF InDir Then
          While Index<FList.Count Do Begin
            IF FileExists(FList.Strings[Index]) Then Inc(Index)
            Else Begin
              SPath:= FList.Strings[Index]+'\';
              Inc(Index);
              Break;
            End;
          End;    IF Index>=Flist.Count Then Break;
      End;  IF (ExcludeAttr And faDirectory)=faDirectory Then
        For Index:=FList.Count-1 DownTo BufferIndex Do
          IF Not FileExists(FList.Strings[Index]) Then FList.Delete(Index);
    end;end.
      

  4.   

    给你一个例子:
    var
      Flist:Tstringlist;
      i:integer;
    begin
      FList:= TStringList.Create;
      SearchFile('C:\abc'+'\*.*', FList, faDirectory, True);
      FileLV.items.BeginUpdate;
      FileLV.items.clear;
      For I:=0 To FList.Count -1 Do
        With FileLV.Items.Add Do
          Caption:= FLIst.Strings[I];
      FileLV.items.EndUpdate;    
      FList.Free;
    End;FileLV 是一个LISTVIEW控件.函数来了:
    unit CusTomFerFuns;Interface
    uses
      Forms,ComCtrls,SysUtils,Windows,ADODB,Mask,stdctrls,Dialogs,Classes; Procedure SearchDirectory(Directory: String; DList: TStringList;
                              ExcludeAttr: Integer; InDir: Boolean);
     Procedure SearchFile(FileName: TFileName; FList: TStringList;
                        ExcludeAttr: Integer; InDir: Boolean);
    implementationProcedure CustomSearchFile(SearchPath: String; List: TStringList;
                               ExcludeAttr: Integer; InDir: Boolean);
    Var
      Index: Integer;
      NextDir: Boolean;
      FindData: TWin32FindData;
      FindHandle: LongWord;
      FileName: TFileName;
    Begin
      IF Copy(SearchPath, Length(SearchPath), 1)<>'\' Then SearchPath:= SearchPath+ '\';
      FindHandle:= FindFirstFile(PChar(SearchPath+ '*.*'), FindData);
      NextDir:= False;
      Index:= List.Count;
      While True Do Begin
        IF NextDir And InDir Then Begin
          FindHandle:=INVALID_HANDLE_VALUE;
          While Index<List.Count Do Begin
            IF Not FileExists(List.Strings[Index]) Then Inc(Index)
            Else
            Begin
              SearchPath:= List.Strings[Index]+'\';
              FindHandle:= FindFirstFile(pChar(SearchPath+ '*.*'), FindData);
              Break;
            End;
          End;
        End;
        IF FindHandle= INVALID_HANDLE_VALUE Then Break;    FileName:= FindData.cFileName;
        IF (FindData.dwFileAttributes And ExcludeAttr)=0 Then
          IF FileName<>'.' Then
            IF FileName<>'..' Then
              List.Add(SearchPath+ FileName);    IF Not FindNextFile(FindHandle, FindData) Then Begin
          windows.FindClose(FindHandle);
          IF InDir Then NextDir:= True Else Break;
        End;
      End;
    End;Procedure SearchDirectory(Directory: String; DList: TStringList;
                              ExcludeAttr: Integer; InDir: Boolean);
    Var
      Index: Integer;
    Begin
      IF ExcludeAttr=-1 Then ExcludeAttr:= faSysFile OR faHidden;
      IF (ExcludeAttr And faDirectory)= faDirectory Then
         ExcludeAttr:= ExcludeAttr Xor faDirectory;
      Index:= DList.Count;
      CustomSearchFile(Directory, DList, ExcludeAttr, InDir);
      For Index:=DList.Count-1 DownTo Index Do
        IF FileExists(DList.Strings[Index]) Then DList.Delete(Index);
    End;Procedure SearchFile(FileName: TFileName; FList: TStringList;
                        ExcludeAttr: Integer; InDir: Boolean);
    Var
      SPath, SFile: String;
      Index, BufferIndex: Integer;
      FindData: TWin32FindData;
      FindHandle: LongWord;
    Begin
      SPath:= ExtractFilePath(FileName);
      SFile:= ExtractFileName(FileName);
      Index:= FList.Count;
      BufferIndex:= Index;
      IF ExcludeAttr=-1 Then ExcludeAttr:= faDirectory OR faSysFile OR faHidden;  While True Do Begin
        IF InDir Then SearchDirectory(SPath, FList, ExcludeAttr, False);
        FindHandle:= FindFirstFile(PChar(SPath+SFile), FindData);
        While FindHandle<>INVALID_HANDLE_VALUE  Do Begin
          FileName:= FindData.cFileName;
          IF (ExcludeAttr=0) OR ((FindData.dwFileAttributes And ExcludeAttr)=0) Then
            IF FileExists(SPath+FileName) Then FList.Add(SPath+FileName);      IF Not FindNextFile(FindHandle, FindData) Then Begin
             FindClose(FindHandle);
             Break;
          End;
        End;
    //
        IF InDir Then
          While Index<FList.Count Do Begin
            IF FileExists(FList.Strings[Index]) Then Inc(Index)
            Else Begin
              SPath:= FList.Strings[Index]+'\';
              Inc(Index);
              Break;
            End;
          End;    IF Index>=Flist.Count Then Break;
      End;  IF (ExcludeAttr And faDirectory)=faDirectory Then
        For Index:=FList.Count-1 DownTo BufferIndex Do
          IF Not FileExists(FList.Strings[Index]) Then FList.Delete(Index);
    end;end.
      

  5.   

    功能强大哦。
    HOHO~~!·
    如果有不明白的地方给我发短消息。
      

  6.   

    TFileListBox中的Items中就存着所有文件
      

  7.   

    FileListBox1直接拖到窗体上,呵呵,多快
      

  8.   

    TFileListBox中的Items中就存着所有文件