Rename files during download
<< Click to Display Table of Contents >> Navigation: Robo-FTP User's Guide > Script Programming > Select Topics in Script Programming > Rename files during download |
The /as option of the RCVFILE command provides a method to download a file such that the local downloaded file has a different name than the same file on the remote server. For example, the colon ":" character is forbidden in Windows file names but allowed in some other operating systems. When using Robo-FTP to download files from a remote server where the colon character is allowed, it is not sufficient to download the files "as-is" and then rename them. They must be renamed DURING the download process. The example Robo-FTP logic below examines the names of every remote file before it is downloaded. When the remote filename contains one or more colon character, they are removed via the RCVFILE [ server name ] /as [ local name ] command syntax.
WORKINGDIR "c:\MyDownloadFolder"
FTPLOGON "ftp.something.com" /user="Fred" /pw="Secret"
:get_file_details
FTPGETFILE "*"
IFERROR GOTO done
;; get the number of colons
SETSUBSTR colons = %sitefile ":"
IFNUM> colons 0 GOTO remove_colons
RCVFILE %sitefile
GOTO get_file_details
:remove_colons
SET local_name = ""
SETNUM segments = colons + 1
SETNUM counter = 1
:fix_name_loop
IFNUM> counter segments GOTO fix_name_done
SETEXTRACT chunk = %sitefile ":" counter
SET local_name = local_name + chunk
INC counter
GOTO fix_name_loop
:fix_name_done
RCVFILE %sitefile /as local_name
GOTO get_file_details
:done
FTPLOGOFF
STOP
Another common requirement is to download a set of files every day from a remote FTP site. The example below demonstrates how to download those files and rename them so the filename includes today's date, thus providing an easy way to maintain an archive of downloaded files. This script uses the FTPGETFILE command in a loop that downloads and renames files that match a wildcard pattern. At the beginning of each loop iteration the value of the %sitefile variable is set to a new file name. Next, the SETRIGHT command is used to split the file's name from its extension. Then, the name is recombined with the value of the %date variable inserted between the name and the extension. Finally, the RCVFILE command is used with its /as option to download the file using the new name.
WORKINGDIR "c:\download\destination\folder"
FTPLOGON "ftp.mydomain.com" /user="UserID" /pw="secret"
IFERROR GOTO done
;; only process files matching this pattern: *.txt
:start_of_loop
FTPGETFILE "*.txt"
IFERROR GOTO done
SET FileName = %sitefile
;; move last 4 characters into Extension
SETRIGHT Extension = FileName 4 /split
SET FileName = FileName + %date + Extension
;; recombine but insert date
RCVFILE %sitefile /as FileName
GOTO start_of_loop
:done
FTPLOGOFF
STOP
The TMPNAME command is useful for simplifying the process of temporarily downloading a file under a different name and then returning the original name once the transfer is a verified success.
Related Command(s): RCVFILE, SETSUBSTR, SETEXTRACT, SET