Delphi FTP TreeView

How can I create a TreeView that displays all directories/files in a FTP site?

I'm using IdFTP for this.   Here's the code I have now, but it's giving me an error when it gets to a subdirectory:

Procedure TForm1.FTPDirToTreeView(AFTP: TidFTP; ATree: TTreeView;
    ADirectory: String; AItem: TTreeNode;
    AIncludeFiles: Boolean);
Var
    ItemTemp: TTreeNode;
    LS: TStringList;
    i: integer;
Begin
    ATree.Items.BeginUpdate;
    LS := TStringList.Create;
    Try
        If ADirectory <> '' Then
            AFTP.ChangeDir(ADirectory);

        AFTP.TransferType := ftASCII;

        AFTP.List(LS);

        If AFTP.DirectoryListing.Count <> 0 Then
        Begin
            For i := 0 To AFTP.DirectoryListing.Count - 1 Do
            Begin
                If AFTP.DirectoryListing.Items[i].ItemType = ditDirectory Then //Here's where I get an out of index error
                Begin
                    AItem := ATree.Items.AddChild(AItem, AFTP.DirectoryListing.Items[i].FileName);
                    ItemTemp := AItem.Parent;
                    FTPDirToTreeView(AFTP, ATree, ADirectory + AFTP.DirectoryListing.Items[i].FileName, AItem, AIncludeFiles);
                    AItem := ItemTemp;
                End
                Else If (AIncludeFiles) Then //if not a directory
                Begin
                    ATree.Items.AddChild(AItem, AFTP.DirectoryListing.Items[i].FileName);
                End;
            End;
        End;

      Finally
        ATree.Items.EndUpdate;
        LS.Free;
    End;
End;

This lists all files until the first subdirectory, but then puts items from the subdirectory into the root directory.  I also get an out of index error on the indicated line.

Thank for your help!

LVL 3
light_bulbAsked:
Who is Participating?
 
Eddie ShipmanAll-around developerCommented:
Then you are going to have to parse the listings. That is what the DirectoryListing property does for you.
Use the second method alone and it should also work for you.
0
 
Eddie ShipmanAll-around developerCommented:
See my article here, it shows two ways with Indy's idFTP:
http://www.delphipages.com/tips/thread.cfm?ID=210
0
 
Eddie ShipmanAll-around developerCommented:
Oh crap, that sure looks like my code...

Looks like you are trying to combine the techniques. You can not do that.
Use one or the other. They both work but you can only use one approach.

0
 
Eddie ShipmanAll-around developerCommented:
It also is dependent upon the way the server returns the file list.
They are not all the same, BTW...
0
 
light_bulbAuthor Commented:
I'll have to look at it a little closer later....I'm leaving on vacation today. :)

I got your first method to work, but it shows the whole ftp description for each item including permissions, modify date, etc.  I need it to just show the file/folder name.

Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.