Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 996
Himadri Avatar asked Jun 12 '10 05:06

Himadri


People also ask

How do you get a list of the names of all files present in a directory in Javascript?

To get a list of the names of all files present in a directory in Node. js, we can call the readdir method. const testFolder = './folder/path'; const fs = require('fs'); fs. readdir(testFolder, (err, files) => { files.

How do I get a list of all files and directories in a given directory in Python?

os. listdir() method gets the list of all files and directories in a specified directory. By default, it is the current directory.


2 Answers

if you use delphi 2010 then you can use tdirectory.getfiles first add ioutils.pas to uses clause then write the following line of code in the 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; 
like image 124
Omair Iqbal Avatar answered Oct 15 '22 08:10

Omair Iqbal


@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; 
like image 44
RRUZ Avatar answered Oct 15 '22 07:10

RRUZ