Script

FTP

The professional tool to automate FTP, SFTP, FTPS and Batch FTP

Syntax:

SYNC(local_dir, remote_dir, method, SUBDIRS, wildcard)

  • local_dir: local directory to be synchronized. This directory must exist before calling SYNC.
  • remote_dir: remote directory to be synchronized. This directory must exist before calling SYNC.
  • method:
    UPLOAD Synchronize remote_dir from local_dir. All files in local_dir that do not exist in remote_dir will be uploaded. If a file exists in both locations it will be uploaded whenever the local_dir file is newer.
    DOWNLOAD Synchronize local_dir from remote_dir. All files in remote_dir that do not exist in local_dir will be downloaded. If a file exists in both locations it will be downloaded whenever the remote_dir file is newer.
    UPLOAD_DELETE Synchronize remote_dir from local_dir. All files in local_dir that do not exist in remote_dir will be uploaded. If a file exists in both locations it will be uploaded whenever the local_dir file is newer. Additionally, all remote_dir files that do not exist in local_dir (orphaned files) will be deleted.
    DOWNLOAD_DELETE Synchronize local_dir from remote_dir. All files in remote_dir that do not exist in local_dir will be downloaded. If a file exists in both locations it will be downloaded whenever the remote_dir file is newer. Additionally, all local_dir files that do not exist in remote_dir (orphaned files) will be deleted.
  • SUBDIRS (optional): Use this parameter if you want ScriptFTP to synchronize subdirectories, too.
  • wildcard (optional): Use this parameter to tell SYNC to synchronize only those files whose names match the wildcard. If this parameter is not used SYNC will synchronize all files.

Remarks:

  • The SYNC command always needs write permissions on the FTP site because it will create a temporary remote file for calculating the client-server time difference. This value is needed to find out whether a local file is newer than its remote counterpart (or vice versa). However, if you want to synchronize a local directory from a remote one (download) and do not have write privileges on the server you can set this time difference manually using the SETCLOCKDIFF command.
  • If DOWNLOAD_DELETE or UPLOAD_DELETE is used and SYNC is unable to delete certain orphaned files or directories it will merely display a warning. It will not stop and throw an error in this case.
  • SYNC will check whether local_dir and remote_dir already exist before starting the synchronization. If this check fails it will display an error and abort.

Command compatibility:

ScriptFTP 2.1 Build Feb 2th 2006: The optional parameter “wildcard” is added.
ScriptFTP 2.2 Build April 4th 2006: The meaning of the third parameter has been changed. The UPLOAD and DOWNLOAD parameters do no longer perform any file deletion, use UPLOAD_DELETE and DOWNLOAD_DELETE instead.

Return Value:

SYNC will return “OK” if all synchronization operations except from deleting orphaned files were successful.
If SYNC fails it will return an error code. You may retrieve the return value and execute various operations depending on this value. See Error Handling.

See also:

GETFILE
PUTFILE
SETCLOCKDIFF

Examples:

# Connect to FTP server
OPENHOST("ftp.myhost.com","myuser","mypassword")
 
# Synchronize a local directory from an FTP site
SYNC("C:\accounting","/accounting",DOWNLOAD,SUBDIRS)
 
# Close connection
CLOSEHOST
# Connect to FTP server using FTP over SSL (secure)
SETPROTOCOL(FTPS_EXPLICIT)
OPENHOST("ftp.myhost.com","myuser","mypassword")
 
# Synchronize a web site from a local directory
SYNC("C:\local_website","/www",UPLOAD_DELETE)
 
# Close connection
CLOSEHOST
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
 
# Synchronize a single file only
SYNC("C:\accounting","/accounting",DOWNLOAD,"accounts.xls")
 
# Close connection
CLOSEHOST
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
 
# Synchronize all Excel files from an FTP site
SYNC("C:\accounting","/accounting",DOWNLOAD,SUBDIRS,"*.xls")
 
# You may also omit the SUBDIRS parameter:
# SYNC("C:\accounting","/accounting",DOWNLOAD,"*.xls")
 
# Disconnect
CLOSEHOST