download files in loop

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
baech
Posts: 10
Joined: Thu 25 Aug 2016 09:30

download files in loop

Post by baech » Wed 21 Sep 2016 14:32

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

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: download files in loop

Post by ViktorV » Thu 22 Sep 2016 14:21

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

Post Reply