Using TFileFind To Populate A TTreeView with Folders

Post here your experiences, comments, suggestions, or bug reports regarding to the DELPHI AREA products. Not-matching topics will be moved or deleted.

Using TFileFind To Populate A TTreeView with Folders

by w2m » 02/04/10 00:26

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: 08/03/03 19:11
Location: New York, USA

Re: Using TFileFind To Populate A TTreeView with Folders

by Kambiz » 19/04/10 08:50

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

Donate a cup of food for free: Click to Give @ The Hunger Site
User avatar
Kambiz
Administrator
Administrator
 
Posts: 1955
Joined: 07/03/03 19:10
Location: Tehran, Iran

Re: Using TFileFind To Populate A TTreeView with Folders

by w2m » 19/04/10 15:21

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: 08/03/03 19:11
Location: New York, USA


Return to DELPHI AREA Products

Who is online

Users browsing this forum: No registered users and 0 guests