Re: Directory Listing in a TTreeView

Giganews Newsgroups
Subject: Re: Directory Listing in a TTreeView
Posted by:  Remy Lebeau (TeamB) (no.spam@no.spam.com)
Date: Thu, 10 Feb 2005

"Lance Robaldo" <RRobal…@wltsoftware.com> wrote in message
news:380D3D5A34BFE240RRobal…@wltsoftware.com...

> Can anyone help me parse the FTP Directory listing into
> a TTreeView in Borland C++ Builder?

There is nothing to parse.  TIdFTP does all of the parsing for you
automatically.  The only thing you have to do yourself is call List() and
then loop through the DirectoryListing collection to fill in the TreeView.
For example:

    AnsiString __fastcall GetFTPPath(TTreeNode *Node)
    {
        AnsiString Result;
        while( Node )
        {
            Result = Result + "/" + Node->Text;
            Node = Node->Parent;
        }
        return Result;
    }

    void __fastcall TForm1::TreeView1Change(TObject *Sender, TTreeNode
*Node)
    {
        TCursor OldCursor = Screen->Cursor;
        Screen->Cursor = crHourGlass;
        try
        {
            ListView->Items->Clear();
            ListView->Items->BeginUpdate();
            try
            {
                IdFTP1->ChangeDir(GetFTPPath(Node));
                IdFTP1->List(NULL, "", true);
                for(int x = 0; x < IdFTP1->DirectoryListing->Count; x++)
                {
                    TIdFTPListItem *Item =
IdFTP1->DirectoryListing->Items[x];
                    if( Item->ItemType == ditFile )
                    {
                        TListItem *ListItem = ListView1->Items->Add();
                        ListItem->Caption = Item->FileName;
                        ListItem->Data = Node;
                    }
                }
                __finally {
                    ListView->Items->EndUpdate();
                }
            }
        }
        __finally {
            Screen->Cursor = OldCursor;
        }
    }

    void __fastcall TForm1::TreeView1Expanding(TObject *Sender, TTreeNode
*Node, bool &AllowExpansion)
    {
        Node->DeleteChildren();
        AllowExpansion = true;
    }

    void __fastcall TForm1::TreeView1Expanded(TObject *Sender, TTreeNode
*Node)
    {
        TCursor OldCursor = Screen->Cursor;
        Screen->Cursor = crHourGlass;
        try
        {
            IdFTP1->ChangeDir(GetFTPPath(Node));
            IdFTP1->List(NULL, "", true);
            for(int x = 0; x < IdFTP1->DirectoryListing->Count; x++)
            {
                TIdFTPListItem *Item = IdFTP1->DirectoryListing->Items[x];
                if( Item->ItemType == ditDirectory )
                    TreeView1->Items->AddChild(Node, Item->FileName);
            }
        }
        __finally {
            Screen->Cursor = OldCursor;
        }
    }

Gambit

Replies

In response to

Directory Listing in a TTreeView posted by Lance Robaldo on Thu, 10 Feb 2005