Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Display List All Files in a Directory

Author: admin | Category: Files

//This is How we can display list all files located in a directory
//This example need a button (Tbutton), DriveComboBox (TDriveComboBox), DirectoryListBox (TDirectoryListBox) and ListBox (TListBox)
// Set the the DirList property of your DriveComboBox1 to DirectoryListBox1
//When Button1 clicked, Listbox1 will display all files in a directory selected in DirectoryListBox1
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, FileCtrl;
 
type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    DirectoryListBox1: TDirectoryListBox;
    Button1: TButton;
    DriveComboBox1: TDriveComboBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure ListFileDir(Path: string; FileList: TStrings);
var
  SR: TSearchRec;
begin
  if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
  begin
    repeat
      if (SR.Attr <> faDirectory) then
      begin
        FileList.Add(SR.Name);
      end;
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  direktori:string;
begin
  direktori:=DirectoryListBox1.Directory;
  ShowMessage(direktori);
  ListBox1.Clear;
  ListFileDir(direktori+'\', ListBox1.Items);
end;
 
end.

Tags: , ,