The “Hot Send” Feature

Top  Previous  Next

 

The GETNEXTFILE and SENDFILE commands can be used together to create a service script that watches a folder on your local computer and automatically uploads any files placed into that folder to a remote site. This type of automatic real-time processing is sometimes known as a "hot send" process.

 

The following example watches a local folder for new files, uploads them as they appear, and then deletes them:

 

LOG "hot_send_script.log" /maxsize=1000

TRACELOG "hot_send_trace.log" /maxsize=1000

WORKINGDIR "c:\upload_source_dir"

IFERROR= $ERROR_SUCCESS GOTO find_file

DASHBOARDMSG "Unable to open source folder." /status=bad

STOP ;; change STOP to EXIT before running as a service

 

:find_file

DASHBOARDMSG "Waiting for next file."

GETNEXTFILE "*" /timeout=0

IFERROR!= $ERROR_SUCCESS GOTO find_file

 

:upload

DASHBOARDMSG "Uploading file: " & %nextfile

FTPLOGON "ftp.new.com" /user=anonymous /pw=itchy

SENDFILE %nextfile

IFERROR!= $ERROR_SUCCESS GOTO upload_error

FTPLOGOFF

DASHBOARDMSG "Upload complete.  Deleting local file."

DELETE %nextfile

GOTO find_file

 

:upload_error

DASHBOARDMSG "Problem uploading file: " & %nextfile

PAUSE /for=3 ;; give user a few seconds to read problem message

FTPLOGOFF

GETNEXTFILE %nextfile

IFERROR= $ERROR_SUCCESS GOTO upload ;; if file exists try to upload

GOTO find_file     ;; file no longer exists so find another file

 

Warning: This example script will upload the same file repeatedly if you don't have sufficient credentials to delete files from the watched folder.

 

The MOVE command may be used to transfer the uploaded file out of the watched folder instead of simply deleting it. You may also consider using the SENDFILE command's /archive or /delete options for this same purpose. The bottom line is you need the uploaded gone from the watched folder so it isn't found again by GETNEXTFILE "*" on the subsequent loop iteration.

 

If you do not wish to remove files from the local folder after they are uploaded you'll need a method of distinguishing new files from existing files and also for recognizing when existing files are modified.  In these situations you should use DIFF and GETDIFF instead of GETNEXTFILE to identify and track which files need to be uploaded.  The DIFF command also supports the the /incldirs option if you need to descend into a subdirectory tree.

 

The sample script above can be used as a starting point for developing more complex scripts. For example you could add logic to ZIP or PGP encrypt the files before uploading them or maybe, after a successful upload you might send an email or insert a row into a database.  

 

 

See also: The "Hot Receive" Feature, Using the %nextfile, %nextpath, and %nextfolder Variables, Programming Service Scripts