Multi-Purpose Scripts
<< Click to Display Table of Contents >> Navigation: Robo-FTP User's Guide > Script Programming > Select Topics in Script Programming > Multi-Purpose Scripts |
It is possible to create a single script file that is used by multiple similar automation jobs when each task is a slight variation on a common theme. When designing such scripts you must identify which data and functionality is common across all jobs and which varies dynamically from job to job. External values are then used to trigger the appropriate script behavior for each permutation of the generic job. Multi-purpose scripts require extra time for development and testing but they are easier to support and maintain than a large number of very similar scripts with small hard-coded differences.
External Values
External values might be used by the script logic to identify files, specify login credentials or even signal that an optional step should be skipped. The easiest way to pass external values into a script is to use command line switches (see Passing External Values Into Command Scripts). Other common techniques including using the READFILE command to parse external values out of a text file or the DBQUERY command to read these values from a database. Each of these techniques causes an external value to be stored in a script variable that is then processed by the multi-purpose script. The variable may then be used in a conditional branch or passed to a script command as an argument or option.
Example Multi-Purpose Script
In the example below we will be discussing a multi-purpose script named UploadFile.s. This script performs the common functionality of uploading a file from a specific folder but both the actual file name and how it is processed after uploading are determined by external values.
WORKINGDIR "e:\staging\outbound\"
FTPLOGON "ftp.mydomain.com" /user="UserID" /pw="secret"
SENDFILE %1
IFERROR GOTO send_failed
FTPLOGOFF
SET ArchivePath = ""
SET ArchivePath = %2
IFSTRCMP ArchivePath "" GOTO done
IFSTRCMPI ArchivePath "delete" GOTO delete_sent_file
SET ArchivePath = ArchivePath + "\" + %1
MOVE %1 ArchivePath
GOTO done
:delete_sent_file
DELETE %1
GOTO done
:send_failed
FTPLOGOFF
MOVE %1 "e:\staging\outbound\error\"
:done
EXIT
Now consider the following command line.
robo-ftp.exe -v -h -s "UploadFile.s" -p1 "20131015Shift1.dat" -p2 "\\backupsvr\sent"
The first item on the command line is the executable for Robo-FTP. Notice that the folder for robo-ftp.exe is not specified because the Robo-FTP installation folder was added to the PATH environment variable (see: Ading Robo-FTP to the Path). The next items are the -v and -h switches which suppress the user-interface so Robo-FTP can run unattended. The script file name follows the -s switch and then -p1 and -p2 pass the dynamic external values.
The value of -p1 is mapped to a variable named %1 and contains the name of a file to be processed, in this example the file is named 20131015Shift1.dat but it could be any file in the working folder specified on the first line. Notice how %1 is passed as a command argument to the SENDFILE, MOVE and DELETE commands. The value of -p2 is mapped to a variable named %2 and contains a path where the file will be moved if it is uploaded without an error.
Dynamic Behavior
This multi-purpose script supports both dynamic file names and also dynamic behavior with regard to how files are processed after they are successfully uploaded. Depending on the value of the %2 variable the file may be left in its current location, deleted, or moved to another folder. The following command line would cause the local copy of the uploaded file to be deleted:
robo-ftp.exe -v -h -s "UploadFile.s" -p1 "20131015Shift1.dat" -p2 "Delete"
Optional Arguments
Review the example script logic above and notice the variable %2 is not passed directly to the IFSTRCMP command. Now consider the following command line:
robo-ftp.exe -v -h -s "UploadFile.s" -p1 "20131015Shift1.dat"
Since the -p2 switch is omitted the variable %2 would not be created. This means that passing %2 directly to the IFSTRCMP command would fail and execution to fall through to the next line. Notice that the variable named ArchivePath is initialized to a default value before it is assigned to the value of %2. This shifts the potential error condition (caused by the missing -p2 switch) to the SET command where it is harmless because that variable has a default value. This technique eliminates the unintended consequences of an error in the conditional branching logic. In the example above the default value of %2 causes the uploaded file to remain in its original location.
Related command(s): EXEC, CALL, CHAIN
See also: Passing External Values Into Command Scripts, Concurrent Script Execution