Board index » delphi » Getting a complete directory list off a drive

Getting a complete directory list off a drive

At the moment I am using the following code to get a directory list. My
problems is that each drive selected will have its own root directories and
the root directories will have their sub directories and so on.

Of course the program isn't going to know this so my question is how do I
get the program to read all subdirectories etc..

Code:

implementation

{$R *.DFM}
{This gets the inital list off drive C}
procedure TForm1.Button1Click(Sender: TObject);
begin
GetDirectories('c:',Memo1);

end;

{This functions retrieves the directories}
procedure GetDirectories(const DirStr : string;
ListBox : TMemo);
var
DirInfo: TSearchRec;
r : Integer;
begin
r := FindFirst(DirStr + '\*.*', FaDirectory, DirInfo);
while r = 0 do begin
Application.ProcessMessages;
if ((DirInfo.Attr and FaDirectory = FaDirectory) and
(DirInfo.Name <> '.') and
(DirInfo.Name <> '..')) then
listbox.lines.Add(DirStr + '\' + DirInfo.Name);
r := FindNext(DirInfo);
end;
FindClose(DirInfo);
end;

{This gets the list from button1 and then finds the first level of
subdirectories}
procedure TForm1.Button2Click(Sender: TObject);
var
counter : integer;
begin
Counter := 0;
while counter < memo1.Lines.Count do
begin
getdirectories(memo1.Lines.Strings[counter],memo2);
inc(counter);
end;

end;

Regards

TimCS

 

Re:Getting a complete directory list off a drive


You could recursively call GetDirectory just as I inserted at line >>>>>>>>
below in your code. It would also be useful to put the code in a TThread.
You would get some speed and still make the application responsive to
messages without calling the ProcessMessages all the time (for which some
say it's a bad thing).
Try searching Torry's or DelphiPages for TFileFind, which does exactly the
thing you need. I believe a threaded version of the component is also
available.
Regards,
G.

Quote
"TimCS" <ca...@sniffout.com> wrote in message

news:a99lp2$qdm$1@newsg4.svr.pol.co.uk...
Quote
> At the moment I am using the following code to get a directory list. My
> problems is that each drive selected will have its own root directories
and
> the root directories will have their sub directories and so on.

> Of course the program isn't going to know this so my question is how do I
> get the program to read all subdirectories etc..

> Code:

> implementation

> {$R *.DFM}
> {This gets the inital list off drive C}
> procedure TForm1.Button1Click(Sender: TObject);
> begin
> GetDirectories('c:',Memo1);

> end;

> {This functions retrieves the directories}
> procedure GetDirectories(const DirStr : string;
> ListBox : TMemo);
> var
> DirInfo: TSearchRec;
> r : Integer;
> begin
> r := FindFirst(DirStr + '\*.*', FaDirectory, DirInfo);
> while r = 0 do begin
> Application.ProcessMessages;
> if ((DirInfo.Attr and FaDirectory = FaDirectory) and
> (DirInfo.Name <> '.') and
> (DirInfo.Name <> '..')) then
> listbox.lines.Add(DirStr + '\' + DirInfo.Name);
>>>>>>>> GetDirectories(DirStr + '\' + DirInfo.Name, ListBox);
> r := FindNext(DirInfo);
> end;
> FindClose(DirInfo);
> end;

> {This gets the list from button1 and then finds the first level of
> subdirectories}
> procedure TForm1.Button2Click(Sender: TObject);
> var
> counter : integer;
> begin
> Counter := 0;
> while counter < memo1.Lines.Count do
> begin
> getdirectories(memo1.Lines.Strings[counter],memo2);
> inc(counter);
> end;

> end;

> Regards

> TimCS

Other Threads