Solved

Using a TTreeView to list directories and files

Posted on 1998-07-12
14
923 Views
Last Modified: 2010-04-04
In Delphi 3 I want to use a TTreeView (under the Win32 component tab) to list subdirectories as the main topics and all of the files in each subdirectory as "children"

How is this done?
0
Comment
Question by:knowlton
  • 6
  • 4
  • 2
  • +2
14 Comments
 

Expert Comment

by:KirkGray
ID: 1357753
OK - well, First You need to understand how a treeview works.

I suggest you read some of the help on custom treeviews..

Basically you will need to clear the treeview. (ttreeview.clear)

Then, Obtain the directory listing by using findfirst and findnext,  add these directories to your treeview using ttreeview.add or addfirst.

Then for each directory you have added use findfirst and findnext again to get all the files for that directory, and use ttreeview.addchild..

This is just a basic outline... if you need more specific help let me know what you don't understand .. ;-)



0
 
LVL 5

Author Comment

by:knowlton
ID: 1357754
This is actually what I am already trying to do...but for some reason it is adding in the names of the subdirectories twice...once by themselves and once with all of the files branching from the directory as they are supposed to.

What I need is some working sample code inside a procedure called InitTreeView or something like that.

Thanks!
0
 

Expert Comment

by:nileq
ID: 1357755
There is a component that works just the way you described. It's under the 'Win31' tab, it's named: FileListBox. This component works very well and will automatically display all of the files and folders
0
 
LVL 5

Author Comment

by:knowlton
ID: 1357756
NILEQ:

I know about and have used the FileListBox.  Yes, it works just fine...but i want a different look for my file management.

The answer needs to show me how to use a TTreeView component to achieve the same functionality as the FileListBox.
0
 

Expert Comment

by:KirkGray
ID: 1357757
If you would care to post the existing code you have I'm sure we could find the problem quicker than coding the hole thing ourselves... It sounds like you are very close to the answer yourself...


0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1357758
Please post some code, and we'll see what's the problem.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357759
Best, in my opinion, will be to use the "File Tree Walk" component (http://sunsite.icm.edu.pl/delphi/ftp/d10free/tftw.zip) to find the files and subfolders quickly, and to post them into a standard treeview and make it look as you want.

Some freeware and shareware components are also available specifically for your task. I hope that one of these should look nice too (just some freeware):

                   http://sunsite.icm.edu.pl/delphi/ftp/d10free/calmir10.zip
" icon based file manager, explorer tree, shortcuts, bin etc.."

                   http://bes.trendline.co.il/torry/vcl/lbcb/flist230.zip
"TFileList is a ListView specialized for file listing in an Explorer-like way."

                   http://home.t-online.de/home/mirbir.st/dirtree.zip
"TreeView to display the system's drives and folders (also custom files)."

                   http://bes.trendline.co.il/torry/vcl/lbcb/expbox.zip
"This component combines both a file listbox and a directory listbox to create an Explorer-style navigation and file selection listbox. Now it looks more like "original" control. Added icons for files."

Lot's of shareware controls are also available from Delphi pages like http://bes.trendline.co.il/torry/

Cheers, bosism@netvision.net.il
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Author Comment

by:knowlton
ID: 1357760
Here is the code:

procedure TFormArtManager.InitTreeViewList;
var
  Found : integer;
  Path : string;
  Search : TSearchRec;
  EXT : string;
begin
  EXT := '*.*';
  Path := ExtractFilePath(Application.ExeName) + 'Groups\';
  Found := SysUtils.FindFirst(Path + EXT, faDirectory, Search);
  if (Found <> 0) then
    ShowMessage('I/O Error # ' + IntToStr(found));
  while Found = 0 do begin
    if (Search.Attr and fadirectory > 0) and
      ((Search.Name <> '.') and (Search.Name <> '..'))then begin
        ListPCXFiles(Search.Name);
      end;
    Found := SysUtils.FindNext(Search);
  end;
  SysUtils.FindClose(Search);
end;

procedure TFormArtManager.ListPCXFiles(Group : string);
var
  Found : integer;
  Path,
  TempFileName : string;
  Search : TSearchRec;
  Node : TTreeNode;
  Parent : TTreeNode;
begin
  Path := ExtractFilePath(Application.ExeName) +
     'Groups\' +
      Group +
      '\';
  Found := SysUtils.FindFirst(Path + '*.pcx', faDirectory , Search);
  Node := TreeViewGroupsAndFiles.Items.AddFirst(nil,Group);
  if (Found <> 0) then
    ShowMessage('I/O Error # ' + IntToStr(found));
  while Found = 0 do begin
    TempFileName := Copy(Search.Name,1,Pos('.',Search.Name) - 1);
    TreeViewGroupsAndFiles.Items.AddChild(Node,TempFileName);
    Found := SysUtils.FindNext(Search);
   end;
  SysUtils.FindClose(Search);
end;

This is what the code produces (in expanded Tree View):

____________________
Tiles  //This is duplicating the parent below!  I don't want "Tiles" to show up twice...
Sprites   //This is duplicating the parent below!  I don't want "Sprites" to show up twice...
-Tiles
  file1
  file2
-Sprites
  file1
  file2
  file3
________________
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357761
Your code won't work if you have some more sub-sub-folders - they won't be dispalyed.
I would suggest using recursive code instead:

procedure TForm1.ListPCX(path: AnsiString; Node: TTreeNode);
var
  srRes : TSearchRec;
  iFound : Integer;
begin
  if path[Length(path)] <> '\' then path := path +'\';

    iFound := FindFirst( path + '*.*', faAnyfile, srRes );
    while iFound = 0 do
    begin
      if ( srRes.Name <> '.' ) and ( srRes.Name <> '..' ) then
        if srRes.Attr and faDirectory > 0 then
          ListPCX(path+srRes.Name, TreeViewGroupsAndFiles.Items.AddChild(Node, {path+}srRes.Name));
      iFound := FindNext(srRes);
    end;
    FindClose(srRes);

  iFound := FindFirst(path+'*.bmp', faAnyFile-faDirectory, srRes);
  while iFound = 0 do
  begin
    if ( srRes.Name <> '.' ) and ( srRes.Name <> '..' ) and ( srRes.Name <> '' ) then
      TreeViewGroupsAndFiles.Items.AddChild(Node,srRes.Name);
    iFound := FindNext(srRes);
  end;
  FindClose( srRes );
end;

PS why did you reject my previous answere?
0
 
LVL 5

Author Comment

by:knowlton
ID: 1357762
Matvey:

I appreciate the work you've put into your response, but I ran into this problem:

How do I make the VERY first call to ListPCXPath?  Node is not set during the first call.

I guess you're dealing with a real newbie here, but none of the responses so far are what I'm looking for.  They are still assuming I know things I don't know.

I need something I can just drop in my code and have it work.

This is what I want:

A procedure that looks like this:

procedure InitTree(directory: string)

directory will contain a path like this:  C:\GROUP\SUBDIR1

InitTree will do the following:

Add SUBDIR1 as a Parent as a node.
Add all files inside of SUBDIR1 as children of that node.

Each subsequent call to InitTree will create a new Parent / Children under a new node.

I am trying to explain this the best I can.  The only difference between what I'm doing and what MS Explorer does is I don't want to display the COMPLETE path to SUBDIR1.  I want to START with SUBDIR1 as the top Parent.

This is how the tree will look:

SUBDIR1
  File1
  File2
  File..Nth
SUBDIR2
  File1
  File2
  File.....Nth
SUBDIR...Nth

Thank you!
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357763
The procedure I posted does exactly what you want. Have you had success running it? You just run it like this: ListPCX('C:\GROUP\SUBDIR1', nil);

It will list exactly the way you want, though it won't clear the items that were in the treeview before - clear them manually and then run the procedure. The nil parameter indicates that the files/directories will start listing from the top of the treeview. If you provide another parameter than nil, the files/directories will start listing from the item you provided.

NOTE: I forgot to change '*.bmp' into '*.pcx' in the procedure.
(iFound := FindFirst(path+'*.bmp', faAnyFile-faDirectory, srRes);)
0
 
LVL 5

Author Comment

by:knowlton
ID: 1357764
Matvey:

You're going to laugh, but I actually found out the problem myself...I had made another call to TreeView somewhere else...and it was causing the duplication.

You've worked so hard on this, and I'm sure your recursive procedure works.  I may still implement it at some point.

Please resubmit your last posting as an answer, and I'll give you the points.

Tom
0
 
LVL 3

Accepted Solution

by:
Matvey earned 60 total points
ID: 1357765
Heh, such things are normal. There was a guy that had three lines of code that were driving him mad, and he posted it as a question here. From the first look it was obvious that he just had a mistake in the order of the commands...

I didn't work hard on this at all. I wrote the recursive procedure for another question that copies files and for another question that displays the registry tree. I just had to change it a bit so it builds the directory tree...

Your procedure has a problem - it will just display one level of subdirectories. If you want to display sub-sub-sub-directories also, you have to use recursion or something else to fix your procedure.

Cheers, Matvey
0
 
LVL 5

Author Comment

by:knowlton
ID: 1357766
Matvey:

I appreciate your encouraging comments.  You are correct...the existing code will not handle sub subdirectories at all.  Right now the program is structured this way on purpose.  It that changes you can be sure I'll be back here looking for that recursive solution you offered!

Being such a beginner is tough sometimes, but I have to start somewhere, right?  I'm a CS major with the bulk of my course work still ahead of me.  Once I get a few of those under my belt, I'll have more a clue more often.

I tacked on another 10 points for you because I'm in a good mood.  Enjoy!

Tom
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now