Using TFileFind To Populate A TTreeView with Folders

Please post bug reports, feature requests, or any question regarding the DELPHI AREA products here.

Using TFileFind To Populate A TTreeView with Folders

Postby w2m » April 2nd, 2010, 12:26 am

I added a TTreeview to the filefind demo as an experiment to see if it is possible to fill a TTreeview with folders similar to ShellTreeview. I thought this would be a very fast way to do this because TFileFind is threaded.

The Treeview is populated with folders except the Treeview levels are not correct. How to detect that the folder name has changed to set the proper TTreeview level.

The code is as shown below.
What am I doing wrong when addtng the nodes?

Code: Select all
procedure TMainForm.FindFileFolderChange( Sender: TObject; const Folder: string; var IgnoreFolder: TFolderIgnore );
var
  Node: TTreenode;
begin
  Inc( Folders );
  StatusBar.SimpleText := Folder;
  if ( Treeview1.Items.Count = 0 ) then
  begin
    { Add the root node }
    with Treeview1.Items.AddFirst( nil, Folder ) do
      Selected := true;
  end
  else
  begin
    Node := Treeview1.Items.AddChild( Treeview1.Selected, Folder );
    Node.Selected := True;
  end;
  if not FindFile.Threaded then
    Application.ProcessMessages;
end;

procedure TMainForm.FindFileFileMatch( Sender: TObject; const FileInfo: TFileDetails );
var
  Node: TTreenode;
begin
  with FoundFiles.Items.Add do
  begin
    Caption := FileInfo.name;
    SubItems.Add( FileInfo.Location );
    if LongBool( FileInfo.Attributes and FILE_ATTRIBUTE_DIRECTORY ) then
    begin
      SubItems.Add( 'Folder' );
      if ( FileInfo.Attributes and faDirectory = faDirectory ) and ( FileInfo.name[ 1 ] <> '.' ) then
      begin
        Node := Treeview1.Items.Add( nil, FileInfo.name );
        Node.Selected := False;
      end;
    end
    else
      SubItems.Add( FormatFileSize( FileInfo.Size ) );
    SubItems.Add( DateTimeToStr( FileInfo.ModifiedTime ) );
  end;
  if not FindFile.Threaded then
    Application.ProcessMessages;
end;
w2m
w2m
Senior Member
Senior Member
 
Posts: 76
Joined: March 8th, 2003, 7:11 pm
Location: New York, USA

Re: Using TFileFind To Populate A TTreeView with Folders

Postby Kambiz » April 19th, 2010, 8:50 am

The following function is not a fast function, but it's the most easiest one.

Code: Select all
function AddFolderToTree(TreeView: TTreeView; const Folder: String): TTreeNode;
var
  ThisFolder: String;
  ParentFolder: String;
  ParentNode: TTreeNode;
  I: Integer;
begin
  ParentFolder := ExcludeTrailingPathDelimiter(ExtractFilePath(ExcludeTrailingPathDelimiter(Folder)));
  ThisFolder := ExtractFileName(ExcludeTrailingPathDelimiter(Folder));
  if ThisFolder = '' then
  begin
    for I := 0 to TreeView.Items.Count - 1 do
      if SameText(TreeView.Items[I].Text, ParentFolder) then
      begin
        Result := TreeView.Items[I];
        Exit;
      end;
    Result := TreeView.Items.AddChild(nil, ParentFolder);
  end
  else
  begin
    ParentNode := AddFolderToTree(TreeView, ParentFolder);
    for I := 0 to ParentNode.Count - 1 do
      if SameText(ParentNode.Item[I].Text, ThisFolder) then
      begin
        Result := ParentNode.Item[I];
        Exit;
      end;
    Result := TreeView.Items.AddChild(ParentNode, ThisFolder);
  end;
end;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Re: Using TFileFind To Populate A TTreeView with Folders

Postby w2m » April 19th, 2010, 3:21 pm

Kambiz,

Thank you for the code... it works very nicely except filling the tree takes to long. I guess I may need to only fill the Tree with drives when it starts and then fill subfolders when a node is expanded. Thank you for your help.

Bill
w2m
w2m
Senior Member
Senior Member
 
Posts: 76
Joined: March 8th, 2003, 7:11 pm
Location: New York, USA


Return to DELPHI AREA Products

Who is online

Users browsing this forum: No registered users and 2 guests

cron