Hi,

I won't be dealing a vast variety of ftp servers. I would like to deal with
directory listing myself.

Component source files were quite overwhelming for me. Anyhow, I finally
managed to get a directory list in TMemoryStream. 

Thanks for turning me to right direction.

My final code is below:

--- BEGIN ---
... // Declare a Form wide variable to get dirlist into
Private
  Var 
    DirStream: TMemoryStream;

...// Assign a TMemoryStream to declared variable
OnFormCreate()
  DirStream := TMemoryStream.Create();

...// Get the directory listing into the variable prepared
procedure TForm10.FtpClient1RequestDone(Sender: TObject; RqType:
TFtpRequest;
  ErrCode: Word);
var
  FtpList: TStringList;
begin
  Memo1.Lines.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', now) + ' On
request:' + IntToStr(Ord(RqType)));

  if ErrCode <> 0 then
  begin
    Memo1.Lines.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', now) + ' ***
ERROR: ' + FtpClient1.ErrorMessage);
    if RqType <> ftpOpenAsync then FtpClient1.QuitAsync;
    Exit();
  end;

  case RqType of
    ftpOpenAsync:    FtpClient1.UserAsync();
    ftpUserAsync:    FtpClient1.PassAsync();
    ftpPassAsync:    FtpClient1.CwdAsync();

    ftpCwdAsync:
    begin
      FtpClient1.HostFileName  := EmptyStr;
      FtpClient1.LocalFileName := EmptyStr;
      FtpClient1.LocalStream   := DirStream; // Key to getting a directory
listing.
      FtpClient1.DirAsync();  // It has to be DirAsync() and not LsAsync()
to get a directory listing
    end;

    ftpDirAsync:
    begin
      if not Assigned(DirStream) then
      begin
        Memo1.Lines.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', now) +
'*** ERROR: LocalStream not assigned');
        Exit();
      end;

      if DirStream.Size = 0 then
      begin
        Memo1.Lines.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', now) +
'*** ERROR: DirStream = 0');
        Exit();
      end;

      DirStream.Seek (0, soFromBeginning) ;
      FtpList := TStringList.Create ;
      try  // finally
        // load FTP file directory, make sure it's not empty
        FtpList.LoadFromStream (DirStream) ;
        Memo1.Lines.Add('-----');
        Memo1.Lines.Add(FtpList.Text);
        Memo1.Lines.Add('-----');
      finally
        FtpList.Free();
      end;

      // When being destroyed, FtpClient component free any
Assigned(LocalStream)
      // this may lead to Access Violation because also we try to free that
variable OnFormDestroy()
      FtpClient1.LocalStream := nil; // Let us Free the private declared
DirStream variable

      FtpClient1.TypeSetAsync();
    end;

    ftpTypeSetAsync:
    begin
      FtpClient1.HostFileName  := edtFilename.Text;
      FtpClient1.LocalFileName := edtLocalPath.Text + '\' +
FtpClient1.HostFileName;
      FtpClient1.GetAsync();
    end;

    ftpGetAsync:
    begin
      FStatus := FtpClient1.StatusCode = 226;
      FtpClient1.QuitAsync();
    end;

    ftpQuitAsync:
    begin
      if FStatus then
        Memo1.Lines.Add('File Download OK')
      else
      begin
        Memo1.Lines.Add('Couldn''t complete download');
        DeleteFile(FtpClient1.LocalFileName);  // Delete the local file if
it's not downloaded completely
      end;
      T2 := GetTickCount();
      Memo1.Lines.Add('Total time: ' + FormatFloat('#,##0 ms', T2-T1));
    end

    else
      Memo1.Lines.Add('Unknown RqType: ' + IntToStr(Ord(RqType)));
  end;
end;

...// Do not forget to Free the allocated memory
OnFormDestroy()
  if Assigned(DirStream) then DirStream.Free();
--- END ---


-----Original Message-----
From: TWSocket [mailto:twsocket-boun...@lists.elists.org] On Behalf Of Angus
Robertson - Magenta Systems Ltd
Sent: Monday, July 25, 2016 12:24 PM
To: twsocket@lists.elists.org
Subject: Re: [twsocket] TFtpClient - listing filenames using stream

> 1-      I do need some help to list files in the FTP server I am 
> connecting. I want to use LocalStream (Please see below)
> 
> 2-      It_s maybe me, but I couldn_t find any sample as to 
> listing of files in a directory using wildcards and excluding 
> directories  (or some how identifying them). Currently, I do not know 
> if I can or how to pass this wildcard to the file listing in order to 
> get only the relevant files listed.

I suggest you look at Magenta Systems File Transfer Components, which are
free components for ICS, that support FTP, HTTP and file copying from single
function calls. 

http://www.magsys.co.uk/delphi/magxfer.asp

You can either save yourself a lot of development testing by using these
components directly, or look at the source code to see how to list file to a
stream and parse the directory.

Note FTP servers do not support wild cards, sub-directories, etc, you need
to do all that work in Delphi.  But the above component does most it, for
instance it gives a list of directory records you can parse to select or
deselect files for download or upload. 

Angus

--
To unsubscribe or change your settings for TWSocket mailing list please goto
http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to