Page 1 of 1

download files in loop

Posted: Wed 21 Sep 2016 14:32
by baech
Hi, I need to be able to download files with a given wildcard char in the name.
Is it possbile to loop over and download all files in a directory where the filename contains with eg "Jan"-

Case 4 files in dir;
budgetjanuary.txt
budgetapril.txt
turnoverjanuary.txt
turnoverapril.txt

i would like to automatically download 'budgetjanuary.txt' and 'turnoverjanuary.txt'

Code: Select all

handle := oSFTPClient.OpenDirectory(ExtractFilePath(ASettings.FileName));
oSFTPClient.ReadDirectory(handle);
Not sure how to loop over the list of files on server directory

Re: download files in loop

Posted: Thu 22 Sep 2016 14:21
by ViktorV
To solve your task, you should use the TScSFTPClient.OnDirectoryList event handler. For example:

Code: Select all

procedure TSFTPClientFrame.ScSFTPClientDirectoryList(Sender: TObject;
  const Path: string; const Handle: TArray<System.Byte>;
  FileInfo: TScSFTPFileInfo; EOF: Boolean);
begin
  if (FileInfo = nil) or (FileInfo.Filename = '.') then
    Exit;

  if Pos('Jan', FileInfo.Filename) > 0 then
   ... save filename to list
end;
The OnDirectoryList event is raised on execution of the TScSFTPClient.ReadDirectory method.
The correct code for using the TScSFTPClient.ReadDirectory method is provided below:

Code: Select all

var
  Handle: TScSFTPFileHandle;
...
  oSFTPClient.SSHClient := oSSHClient;
  oSFTPClient.Initialize;
  Handle := oSFTPClient.OpenDirectory('\');
  try
    while not oSFTPClient.EOF do
      oSFTPClient.ReadDirectory(Handle);
  finally
    oSFTPClient.CloseHandle(Handle);
  end;

  ... download using list of filename