The professional tool to automate FTP and secure FTP transfers. Automated FTP & Batch FTP

Lesson 5: Handling file lists

Sometimes it is useful to perform different actions for each item in a set of files. For example, if you do the following:

# Connect to server
OPENHOST("ftp.myhost.com","joe","123456")
 
# Download all files
GETFILE(*.*)

# Delete the downloaded files   
DELETEFILE(*.*)

# Close the connection
CLOSEHOST

It may happen that GETFILE fails and remote files will be deleted although they have never been downloaded. In order to avoid this issue we will retrieve a file listing with the GETLIST command, call GETFILE for each file in the listing and delete this remote file using DELETEFILE if no error has occured.

# Connect to server
OPENHOST("ftp.myhost.com","joe","123456")

# Change the current local directory. All files
# will be downloaded here.
LOCALCHDIR("C:\users\carlos\desktop\localftp")

# Get the remote file listing, store it in $list
GETLIST($list,REMOTE_FILES)

# For each file in $list...
FOREACH $item IN $list
       # Download the file
       $result=GETFILE($item)

       # If the file has been downloaded successfully
       # delete the remote copy. Else stop the script.
       IF($result=="OK")
               DELETEFILE($item)
       ELSE
               STOP
       END IF
END FOREACH

# Close the connection
CLOSEHOST

GETLIST commands are usually followed by FOREACH loops. See GETLIST for further reference about parameters and options.