Board index » delphi » Newbie Q: Getting a list of files in a directory

Newbie Q: Getting a list of files in a directory

Hi all,

Okay, I've pored over Mastering Delphi and the online docs, but I'm
stumped on this (and I know it must be simple!).

Assuming I have the name of a directory, such as 'C:/Program Files/',
how do I go about getting the names of all the items in that
directory? (I'm guessing as an array of Strings).

I can see how to do this using visual components, but there must be an
easier way...

Cheers,

Tim.

--
"I found my wife in bed {*word*192} one day next to a Vietnamese guy and a
black guy. I took a picture and sent it to Benetton. You never know."
- Franck Dubosc

 

Re:Newbie Q: Getting a list of files in a directory


Use FindFirst, FindNext and FindClose, e.g.:

var
  F: TSearchRec;
begin
  if FindFirst('C:\Program Files\*.*', faAnyFile and not (faDirectory or
faVolumeID), F) = 0 then
  try
    repeat
      ListBox1.Items.Add(F.Name);
    until FindNext(F) <> 0;
  finally
    FindClose(F);
  end;
end;

"Tim Wright" <Tim.Wri...@informa.com.invalid> schreef in bericht
news:Tim.Wright-CEDCE1.11401918122000@enews.newsguy.com...

Quote
> Hi all,

> Okay, I've pored over Mastering Delphi and the online docs, but I'm
> stumped on this (and I know it must be simple!).

> Assuming I have the name of a directory, such as 'C:/Program Files/',
> how do I go about getting the names of all the items in that
> directory? (I'm guessing as an array of Strings).

> I can see how to do this using visual components, but there must be an
> easier way...

> Cheers,

> Tim.

> --
> "I found my wife in bed {*word*192} one day next to a Vietnamese guy and a
> black guy. I took a picture and sent it to Benetton. You never know."
> - Franck Dubosc

Re:Newbie Q: Getting a list of files in a directory


In article <91l14n$5q...@porthos.nl.uu.net>, "M.H. Avegaart"

Quote
<avegaartNOS...@mccomm.nl> wrote:
>Use FindFirst, FindNext and FindClose, e.g.:

>var
>  F: TSearchRec;
>begin
>  if FindFirst('C:\Program Files\*.*', faAnyFile and not (faDirectory or
>faVolumeID), F) = 0 then
>  try
>    repeat
>      ListBox1.Items.Add(F.Name);
>    until FindNext(F) <> 0;
>  finally
>    FindClose(F);
>  end;
>end;

Many thanks. I did in fact find some docs on FindFirst and FindNext,
but the masks and so on were rather intimidating... your example is
exactly what I needed!

Cheers,

Tim.

--
If you want to inspire confidence, give plenty of statistics. It does
not matter that they should be accurate, or even intelligible, as long
as there is enough of them.

Other Threads