Board index » delphi » FTP won't list directories or files...

FTP won't list directories or files...

In the following code, you will see that I have attempted to use the .list
command to attempt to get a directory listing, then output it to a memo
box. It spits out 63 blank lines instead. what's up with that, and how do
I fix it?

Thanks in advance,

-Jon Hartman

=================Begin pasted code=========

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  OleCtrls, isp3, StdCtrls, ComCtrls, TrCtrls, ExtCtrls, dbtables;

type
  TForm1 = class(TForm)
    Button1: TButton;
    TrGroupBox1: TTrGroupBox;
    TrGroupBox2: TTrGroupBox;
    edtport: TEdit;
    edthost: TEdit;
    edtlogin: TEdit;
    edtpassword: TEdit;
    FTP1: TFTP;
    edttemp: TEdit;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Timer1: TTimer;
    mmofiles: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FTP1ListItem(Sender: TObject; const Item: FTPDirItem);
    procedure FTP1DocOutput(Sender: TObject; const DocOutput: DocOutput);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  strtemp: string;
  strlisting: string;
  dirdata: pstring;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with ftp1 do begin
    Connect(edtHost.Text, strtoint(edtport.text));
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ftp1.Authenticate(edtlogin.Text, edtpassword.Text);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
{The following msgdlgs let me know how many lines were returned}
messagedlg(inttostr(mmofiles.lines.count), mtinformation, [mbok], 0);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  DisposeStr(dirData);
  dirData := NullStr;
ftp1.list(DirData^);
end;

procedure TForm1.FTP1ListItem(Sender: TObject; const Item: FTPDirItem);

begin
AssignStr(DirData, strlisting);
mmofiles.lines.add(strlisting);
end;

procedure TForm1.FTP1DocOutput(Sender: TObject;
  const DocOutput: DocOutput);
begin
if timer1.enabled = true then
     messagedlg('DocOutput Event Triggered', mtinformation, [mbok], 0);
if (timer1.enabled = true) and (strlisting = '') then
   messagedlg('Its empty. :(', mtinformation, [mbok], 0);
timer1.enabled := false;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
edttemp.text := inttostr(ftp1.operation);
end;

end.

==============================================================================
||||||||"If you can't take the heat, STAY OUT OF THE FIREWALL!" -Me ||||||||||
==============================================================================
||Out the Token Ring, through the router, down the fiber, off another router,|
||||||||||| down the T1, past the firewall...nothing but Net.|||||||||||||||||
==============================================================================  

 

Re:FTP won't list directories or files...


Quote
Jonathan M. Hartman wrote in message ...
>In the following code, you will see that I have attempted to use the .list
>command to attempt to get a directory listing, then output it to a memo
>box. It spits out 63 blank lines instead. what's up with that, and how do
>I fix it?

>Thanks in advance,

>-Jon Hartman

>=================Begin pasted code=========
>>SNIP!<<
>procedure TForm1.Button4Click(Sender: TObject);
>begin
>  DisposeStr(dirData);
>  dirData := NullStr;
>ftp1.list(DirData^);
>end;

>procedure TForm1.FTP1ListItem(Sender: TObject; const Item: FTPDirItem);

>begin
>AssignStr(DirData, strlisting);
>mmofiles.lines.add(strlisting);
>end;
>>SNIP!<<

The problem seems to be that you are using DirData as if it were the list
item return mechanism, and according to the docs it isn't.  The parameter of
the List method is a string denoting the host directory of which you want a
listing.  The directory items are returned in the Item parameter to the
ListItem event handler.  In other words:

--------------------
procedure TForm1.FTP1ListItem(Sender: TObject; const Item: FTPDirItem);

begin
mmofiles.lines.add(Item.FileName);
end;
--------------------

Note: The structure for FTPDirItem uses WideStrings for the FileName.  I've
never worked with them before, so I'm not sure if you need more gymnastics
to use the FileName property.  I don't have the OCX internet tools
installed, so I can't test to find out either.  Sorry!

-Chris

Other Threads