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:
The PUTFILE command obeys the same syntax. Let us look at an example:
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:
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)
Next Lesson: Variables