Delphi - how to get a list of all files of directory
Asked Answered
P

6

28

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.

Publicity answered 12/6, 2010 at 5:6 Comment(0)
T
33

@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;
Tafia answered 12/6, 2010 at 5:47 Comment(5)
I think your code needs a try finally protecting FindFirst/FindClose(SR).Drome
found a bug you need to replace if FindFirst('.', faArchive, SR) = 0 then with if FindFirst(Path+'.', faArchive, SR) = 0 then because it doesn't look at the pathFilar
How to sort the list by the date of the files ?Langdon
FindFirst is quite outdated. Why not TDirectory.GetFiles ?Ruddle
this code problem in win10Sicyon
F
56

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;
Fiddlehead answered 12/6, 2010 at 6:41 Comment(4)
Good option but I am not using delphi 2010... :-(Publicity
Don't forget to include : uses IOUtils;Whitehorse
TDirectory.GetFiles accepts an additional argument (the first one is the folder) with file mask, were one can get files e.g. with certain extension or matching certain pattern in the filename.Logogriph
I'm using Delphi XE4 (still!) and TDirectory is available.Normally
T
33

@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;
Tafia answered 12/6, 2010 at 5:47 Comment(5)
I think your code needs a try finally protecting FindFirst/FindClose(SR).Drome
found a bug you need to replace if FindFirst('.', faArchive, SR) = 0 then with if FindFirst(Path+'.', faArchive, SR) = 0 then because it doesn't look at the pathFilar
How to sort the list by the date of the files ?Langdon
FindFirst is quite outdated. Why not TDirectory.GetFiles ?Ruddle
this code problem in win10Sicyon
Y
3

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.

Ypres answered 12/6, 2010 at 5:21 Comment(2)
I am not selecting a directory but a file.. and I want other file list of parent directory of selected file.. read the question carefully...Publicity
In that case, CaldonCZE has the answer.Glyceryl
E
2

You can use extractFilePath function to get the directory name:

myPath := extractFilePath(FileName);

where FileName is name of file you choose by OpenDialog.

Endbrain answered 12/6, 2010 at 5:44 Comment(0)
P
1
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.

Paviour answered 2/8, 2019 at 20:48 Comment(0)
B
0

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;
Biddle answered 24/12, 2021 at 21:2 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ideatum

© 2022 - 2024 — McMap. All rights reserved.