Quantcast
Channel: Syncplify.me
Viewing all articles
Browse latest Browse all 144

Monitor a directory, and upload/archive files as they arrive

$
0
0

Monitoring a directory for certain files, and as soon as they become available (someone puts them in that directory) upload them somewhere else and then move the original files to a different location (archive) on the local disk. This is one of the most common questions from our FTP Script! users.

For such reason we have prepared the sample script below. It will probably fit the most common cases, and it’s a decent learning tool as well as starting point to create your own (more complex) scripts to accomplish your very own particular task. 

const
  // Related to files and directories
  FilesToMonitor = 'C:\Projects\*.*';
  RemotePath = '/archivedfiles/';
  LocalArchive = 'C:\Destination\';
  // Related to SFTP server connectivity
  SHost = 'sftp.remoteserver.com';
  SPort = 22;
  SUser = 'sftpusername';
  SPass = 'sftppassword';

var
  DirList, ToDelete: TStringList;
  I: integer;
  Cli: TSFTPClient;

begin
  DirList := TStringList.Create;
  while true do
  begin
    DirList.Clear;
    if FileEnum(FilesToMonitor, DirList, true) then
    begin
      if (DirList.Count > 0) then
      begin
        Log('Found '+IntToStr(DirList.Count)+' files');
        ToDelete := TStringList.Create;
        Cli := TSFTPClient.Create;
        try
          Cli.ServerAddr := SHost;
          Cli.ServerPort := SPort;
          Cli.Username := SUser;
          Cli.Password := SPass;
          if Cli.Open then
          begin
            for I := 0 to DirList.Count-1 do
            begin
              Log('Processing file #'+IntToStr(I+1)+': '+DirList[I]);
              begin
                if Cli.Upload(DirList[I], RemotePath+ExtractFilename(DirList[I]), false, feaOverwrite, ftmCopy) then
                begin
                  if FileCopy(DirList[I], LocalArchive+ExtractFilename(DirList[I]), true) then
                  begin
                    ToDelete.Add(DirList[I]);
                    Log('File uploaded and archived: '+ToDelete[I]);
                  end;
                end;
              end;
            end;
            Cli.Close;
            // We not files to be deleted and delete them all at once after disconnecting from the
            // server, because as long as we are connected such files may still be open/locked by
            // the operating system.
            for I := 0 to ToDelete.Count-1 do
              if FileDelete(ToDelete[I]) then
                Log('File deleted: '+ToDelete[I]);
          end;
        finally
          Cli.Free;
          ToDelete.Free;
        end;
      end
      else
        Log('No files matching the pattern/mask were found, will recheck in 1 second');
    end;
    Sleep(1000);
  end;
  DirList.Free;
end.

The code here above is provided “as is” with no guarantee that it will work in any specific customer environment, use it at your own risk.


Viewing all articles
Browse latest Browse all 144

Trending Articles