LIST FOLDERS

I want to make list for all the folders in application folder (path ).
This list will show in listbox.
sal1150Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

snehanshuCommented:
sal1150,
  The easiest way to display directories is to use TDirectoryListBox found under Win 3.1 tab.
  Just drop a DirectoryListBox on your form and write this code in form-create:

DirectoryListBox1.Directory := ExtractFileDir((Application.ExeName));
 

HEH,
...Shu
snehanshuCommented:
Well, or else, you can do this also (if you want to use a list box:

procedure TForm1.Button1Click(Sender: TObject);
Var
AppDir, CurDir: string;
found: integer;
DirSr: TSearchRec;

begin

  AppDir := ExtractFileDir((Application.ExeName));

  Found:=FindFirst(AppDir+'\*.*', faDirectory, DirSr);
    while Found=0 do
      begin
        if ((DirSr.Attr and faDirectory)=faDirectory) and ((DirSr.Name<>'.') and (DirSr.Name<>'..')) then
        Begin
          ListBox1.Items.Add(DirSr.Name);
//          ListBox1.Items.Add(AppDir+'\'+DirSr.Name);//Full path

        End;
//
        Found:= FindNext(DirSr);
      end;

end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zion29Commented:
You forgot FindClose... :)

     end;
     FindClose(sr);
end;
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

sal1150Author Commented:
HI snehanshu,
your first post is ok but how let DirectoryListBox1 to show only the folders in app dir.(only folders ).

second post is nice but  how can i put lable to show full path for selecting folder.
i tary this :

Var
AppDir, CurDir: string;
found: integer;
DirSr: TSearchRec;

procedure TForm1.Button1Click(Sender: TObject);

begin

  AppDir := ExtractFileDir((Application.ExeName));

  Found:=FindFirst(AppDir+'\*.*', faDirectory, DirSr);
    while Found=0 do
      begin
        if ((DirSr.Attr and faDirectory)=faDirectory) and ((DirSr.Name<>'.') and (DirSr.Name<>'..')) then
        Begin
          ListBox1.Items.Add(DirSr.Name);
        End;
        Found:= FindNext(DirSr);
      end;
end;


procedure TForm1.ListBox1Click(Sender: TObject);
begin
//Label2.Caption :=AppDir+'\'+DirSr.Name;
//Label2.Caption :=AppDir+'\'+listbox1.items.text;
end;
sal1150Author Commented:
aha

i find the answer

procedure TForm1.ListBox1Click(Sender: TObject);
begin
Label2.Caption :=AppDir+'\'+ListBox1.Items[ListBox1.ItemIndex];
end;
snehanshuCommented:
sal1150,
  I am glad that I could help.
  Note that as pointed out by zion 29, you have to add FindClose also.
  Your code should be:
procedure TForm1.Button1Click(Sender: TObject);
Var
found: integer;
DirSr: TSearchRec;
AppDir: string;

begin

  AppDir := ExtractFileDir((Application.ExeName));

  ListBox1.Items.Clear;
  Found:=FindFirst(AppDir+'\*.*', faDirectory, DirSr);
    while Found=0 do
      begin
        if ((DirSr.Attr and faDirectory)=faDirectory) and ((DirSr.Name<>'.') and (DirSr.Name<>'..')) then
          ListBox1.Items.Add(DirSr.Name);
        Found:= FindNext(DirSr);
      end;
    FindClose(DirSr);  
end;


Cheers!
...Shu
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.