Script

FTP

The professional tool to automate FTP, SFTP, FTPS

and schedule FTP batch jobs

Lesson 2: Transferring files

The commands GETFILE, PUTFILE and SYNC are used for file transfers. Since the purpose of ScriptFTP is transferring files, it is important to understand the use of these commands before you write your own script.

The GETFILE command is used for downloading a set of files from the FTP server. It has two parameters – the first one indicates the file or files you want to download. If you also need to download files from subdirectories add the second parameter: SUBDIRS.

# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
 
# Download a.jpg
GETFILE("a.jpg")
 
# Download every file in cgi-bin
GETFILE("cgi-bin/*.*")
 
# Download backup-2005-12-3.zip from backup
GETFILE("/backup/backup-2005-12-3.zip")
 
# Download all files in the directory images
GETFILE("/images/*.*",SUBDIRS)
 
# Close connection
CLOSEHOST

Note that when using the SUBDIRS parameter ScriptFTP will preserve the original directory structure and will therefore create the appropriate directories on the local drive.

Other commonly used script commands are CHDIR and LOCALCHDIR. The first one sets the current remote directory and the second one is used to set the current local directory. ScriptFTP will download all files to the current local directory so you had better use LOCALCHDIR before calling GETFILE. Let us look at an example demonstrating the use of both commands:

# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
 
# Set current local directory
LOCALCHDIR("C:\dest_dir")
 
# Set current remote directory
CHDIR("/images")
 
# Download all files from /images to
# the directory images in C:\dest_dir
GETFILE("*.*",SUBDIRS)
 
# Close connection
CLOSEHOST

The PUTFILE command obeys the same syntax. Let us look at an example:

# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
 
# Set current local directory
LOCALCHDIR("C:\orig_dir")
 
# Set current remote directory 
CHDIR("/images")
 
# Upload all files from C:\orig_dir to
# /images
PUTFILE("*.*",SUBDIRS)
 
# Close connection
CLOSEHOST

And finally the SYNC command. Use this command for synchronizing directories. In ScriptFTP the term syncronization means “Get an exact copy of a directory transferring new and modified files only. Delete unwanted files.”. This definition of syncronization may sound complex, but you will understand it as soon as you see the SYNC command in action:

# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
 
# Synchronize C:\dest_dir from /www
SYNC("C:\dest_dir","/www",DOWNLOAD)
 
# Close connection
CLOSEHOST

The first parameter indicates a local directory, the second one a remote directory and the third one the direction of the synchronization. You can synchronize both a local directory with a remote directory (DOWNLOAD) and a remote directory with a local one (UPLOAD). For example, if you want to publish a web site the command is: SYNC(“C:\local_webdir”,”/www”,UPLOAD)

The SYNC command also has a fourth parameter which is optional: SUBDIRS. Its meaning is the same as in GETFILE or PUTFILE.

For further information take a look at the commands help reference: GETFILE, PUTFILE, SYNC. Also see Transferring modified files only.

Next Lesson: Variables