FTP DirectoryListing question

Giganews Newsgroups
Subject: FTP DirectoryListing question
Posted by:  eshipman (eshipm…@yahoo!!!.com)
Date: Tue, 18 Nov 2003

How can I do this:

procedure FTPDirToTreeView(AUserName, APassword: String; 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 AnsiPos('total', LS[0]) > 0 then
      LS.Delete(0);
    LS.Sorted := True;
    if LS.Count <> 0 then
    begin
      for i := 0 to LS.Count - 1 do
      begin
        try
          if Pos('.', LS.Strings[i]) = 0 then
          begin
            AItem := ATree.Items.AddChild(AItem,
              Trim(Copy(LS.Strings[i],
                  Pos(':', LS.Strings[i]) + 3,
                  Length(LS.Strings[i])) + '/'));
            ItemTemp := AItem.Parent;
            FTPDirToTreeView(AFTP, ATree,
                            ADirectory +
              Trim(Copy(LS.Strings[i],
                  Pos(':', LS.Strings[i]) + 3,
                  Length(LS.Strings[i]))) + '/',
                  AItem, AIncludeFiles);
            AItem := ItemTemp;
          end
          else
            if (AIncludeFiles) and (Pos('.', LS.Strings[i]) <> 0) then
              ATree.Items.AddChild(AItem, LS.Strings[i]);
        except
        end;
      end;
  finally
    LS.Free;
    ATree.Items.EndUpdate;
  end;
end;

using the DirectoryListing, instead of the LS stringlist?

This gives me an index out of bounds error:

procedure FTPDirToTreeView(AFTP: TidFTP; ATree : TTreeView; ADirectory :
string;
                          AItem : TTreeNode; AIncludeFiles : Boolean);
var
  ItemTemp : TTreeNode;
  LS : TStringList;
  i : integer;
  DirList: TIdFTPListItems;
begin
  ATree.Items.BeginUpdate;
  LS := TStringList.Create;
  try
    if ADirectory <> '' then
      AFTP.ChangeDir(ADirectory);
    AFTP.TransferType := ftASCII;
    AFTP.List(LS);
    DirList := AFTP.DirectoryListing;
    for i := 0 to DirList.Count-1 do
    begin
      if DirList.Items[i].Data[1] in ['d', 'D'] then
      // This is  Directory
      begin
        AItem := ATree.Items.AddChild(AItem,
                  Trim(DirList.Items[i].FileName) + '/');
        ItemTemp := AItem.Parent;
        FTPDirToTreeView(AFTP, ATree, ADirectory +
                Trim(DirList.Items[i].FileName) + '/',
                AItem, AIncludeFiles);
        AItem := ItemTemp;
      end
      else
      begin
        if (AIncludeFiles) then
          ATree.Items.AddChild(AItem, Trim(DirList.Items[i].FileName));
      end;
    end;
  finally
    ATree.Items.EndUpdate;
    LS.Free;
  end;
end;

Do I really need the LS StringList at all?

Replies