Board index » delphi » Newbie : Just displaying Directorys and not files in a listbox

Newbie : Just displaying Directorys and not files in a listbox

result:=findfirst ('*',$10,dirinfo);
while result=0 do
Begin
 if ((dirinfo.name<>'.') and (dirinfo.name<>'..')) then Listbox1.items.add
(dirinfo.name);
 result:=findnext(dirinfo);
end;

Can someone please put me out of my mysery !!   I get directorys and files
in my listbox....  Many thanks !

 

Re:Newbie : Just displaying Directorys and not files in a listbox


Quote
Dale Robinson wrote:

> result:=findfirst ('*',$10,dirinfo);
> while result=0 do
> Begin
>  if ((dirinfo.name<>'.') and (dirinfo.name<>'..')) then Listbox1.items.add
> (dirinfo.name);
>  result:=findnext(dirinfo);
> end;

> Can someone please put me out of my mysery !!   I get directorys and files
> in my listbox....  Many thanks !

I wonder why files are returned too ($10 is already specified in
FindFirst) but this code only returns directories.

  Result:=FindFirst('c:\*.*',$10,SearchRec);
  while Result=0 do
    begin
      if SearchRec.Attr=$10 then ListBox1.Items.Add(SearchRec.Name);
      Result:=FindNext(SearchRec);
    end;
  FindClose(SearchRec);

Alex
--
** Pepsi Cola and Animal protection **
**  http://www.pepsi{*word*76}bath.com/  **

Author of the free Chatsystem PINO!
Available at http://pino.cjb.net

Re:Newbie : Just displaying Directorys and not files in a listbox


If you look in the help you will see the following:
"The Attr parameter specifies the special files to include in addition to
all normal files."
That's why.

Try change your code to:

if FindFirst ('*', faDirectory, dirinfo) = 0 then
begin
  repeat
    if (dirinfo.name<>'.') and (dirinfo.name<>'..') then
      if dirinfo.Attr and faDirectory > 0 then
        Listbox1.items.add(dirinfo.name);
  until FindNext(dirinfo) <> 0;
  FindClose(dirinfo);
end;

Finn Tolderlund

Dale Robinson <dale.robin...@bigfoot.com> skrev i en
nyhedsmeddelelse:8175mg$p5...@neptunium.btinternet.com...

Quote
> result:=findfirst ('*',$10,dirinfo);
> while result=0 do
> Begin
>  if ((dirinfo.name<>'.') and (dirinfo.name<>'..')) then Listbox1.items.add
> (dirinfo.name);
>  result:=findnext(dirinfo);
> end;

> Can someone please put me out of my mysery !!   I get directorys and files
> in my listbox....  Many thanks !

Re:Newbie : Just displaying Directorys and not files in a listbox


SendMessage(ListBox1.Handle,LB_DIR,0,DDL_DIRECTORY OR DDL_EXCLUSIVE);

I hope this helps. I might have the last two parameters backwards, so
you might want to look under LB_DIR in the win32 help file.

Tom

On Sat, 20 Nov 1999 22:25:58 GMT, Alexander Mueller <al...@gmx.at>
wrote:

Quote

>Dale Robinson wrote:

>> result:=findfirst ('*',$10,dirinfo);
>> while result=0 do
>> Begin
>>  if ((dirinfo.name<>'.') and (dirinfo.name<>'..')) then Listbox1.items.add
>> (dirinfo.name);
>>  result:=findnext(dirinfo);
>> end;

>> Can someone please put me out of my mysery !!   I get directorys and files
>> in my listbox....  Many thanks !

>I wonder why files are returned too ($10 is already specified in
>FindFirst) but this code only returns directories.

>  Result:=FindFirst('c:\*.*',$10,SearchRec);
>  while Result=0 do
>    begin
>      if SearchRec.Attr=$10 then ListBox1.Items.Add(SearchRec.Name);
>      Result:=FindNext(SearchRec);
>    end;
>  FindClose(SearchRec);

>Alex
>--
>** Pepsi Cola and Animal protection **
>**  http://www.pepsi{*word*76}bath.com/  **

>Author of the free Chatsystem PINO!
>Available at http://pino.cjb.net

Re:Newbie : Just displaying Directorys and not files in a listbox


Quote
In article <3837209B.98A33...@gmx.at>, Alexander Mueller <al...@gmx.at> writes:
>I wonder why files are returned too ($10 is already specified in
>FindFirst) but this code only returns directories.

The attribute of faDirectory (and named constants are better than "magic
numbers" any day) returns directory files AS WELL AS ordinary files. Same for
any other attribute. An attribute of zero returns only ordinary files.

After the function returns you have to check for it being a directory (using
the same attribute) before adding the name to the ListBox. :-

if (DirInfo.Name[1] <> '.') and (DirInfo.Attr = faDirectory) then
  ListBox1.Items.Add(DirInfo.Name);

This is what Alexander effectively did with his code.

Alan Lloyd
alangll...@aol.com

Other Threads