Himadri
Himadri

Reputation: 3012

Delphi - how to get a list of all files of directory

I am working with delphi, I want a list of all files of a directory when I execute openpicturedialog.

i.e., When open dialog is executed and i select one file from it, I want the list of all files from the directory of selected file.

You can even suggest me for getting directory name from FileName property of TOpenDialog
Thank You.

Upvotes: 28

Views: 98229

Answers (6)

Omair Iqbal
Omair Iqbal

Reputation: 1838

If you're using Delphi 2010 then you can use TDirectory.GetFiles.

First add IOUtils to your uses clause, then use the following in your event handler (in addition to code you already have in that event handler):

uses IOUtils;

var
   path: string;
begin
   for Path in TDirectory.GetFiles(OpenPictureDialog1.filename)  do
      Listbox1.Items.Add(Path); // assuming OpenPictureDialog1 is the name you gave to your OpenPictureDialog control
end;

Upvotes: 56

Marcelo Fortes
Marcelo Fortes

Reputation: 11

With this code, you can get the "path" information of the files in the folder you want. You can use Delphi's System.IOUtils library for this.

uses 
...
 System.IOUtils;
...

var List : TStringlist;
var File : String := '';
var Path : string := IncludeTrailingPathDelimiter(Edit1.Text);

Lista := TStringList.Create;
try
    for File in TDirectory.GetFiles(Path) do
        List.Add(File); // Add all file names to list
finally
    FreeAndNil(Lista);
end;

Upvotes: 0

Jens Björnhager
Jens Björnhager

Reputation: 5649

Change the filter property in your OpenPictureDialog to include all files:

All (*.*)

Edit: I don't think you can select a directory in a Open(Picture)Dialog, it surely isn't the purpose of an OpenPictureDialog anyway.

Then use FindFirst and FindNext to get the files in this dir.

Upvotes: 3

Petrus
Petrus

Reputation: 69

if OpenPictureDialog1.Execute then  
  FileListBox1.Directory := extractFilePath(OpenPictureDialog1.FileName);

You can also use a FilterComboBox linked to FileListBox to filter the file type.

TFileListBox and TFilterComboBox are in the tool palette under "Win 3.1". From Delphi 4 there are these objects.

Upvotes: 1

RRUZ
RRUZ

Reputation: 136391

@Himadri, the primary objective of the OpenPictureDialog is not select an directory, anyway if you are using this dialog with another purpose you can try this code.

Var
  Path    : String;
  SR      : TSearchRec;
  DirList : TStrings;
begin
  if OpenPictureDialog1.Execute then
  begin
    Path:=ExtractFileDir(OpenPictureDialog1.FileName); //Get the path of the selected file
    DirList:=TStringList.Create;
    try
          if FindFirst(Path + '*.*', faArchive, SR) = 0 then
          begin
            repeat
                DirList.Add(SR.Name); //Fill the list
            until FindNext(SR) <> 0;
            FindClose(SR);
          end;

     //do your stuff

    finally
     DirList.Free;
    end;
  end;

end;

Upvotes: 33

Ondra C.
Ondra C.

Reputation: 2522

You can use extractFilePath function to get the directory name:

myPath := extractFilePath(FileName);

where FileName is name of file you choose by OpenDialog.

Upvotes: 2

Related Questions