• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1037
  • Last Modified:

Using a TTreeView to list directories and files

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
Tom Knowlton
Asked:
Tom Knowlton
  • 6
  • 4
  • 2
  • +2
1 Solution
 
KirkGrayCommented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
 
nileqCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Tom KnowltonWeb developerAuthor Commented:
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
 
KirkGrayCommented:
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
 
ronit051397Commented:
Please post some code, and we'll see what's the problem.
0
 
MatveyCommented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
 
MatveyCommented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
 
MatveyCommented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
 
MatveyCommented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now