FTP refresher part II - automate the iSeries file transfer

Now we can expand some on the basic information found in FTP Refresher part I - iSeries file transfer made easy.  Refer to part one if a quick review is necessary.

Using script files (file extension ".ftp") is one simple way to accomplish FTP automation.  There are many software packages that support automated FPT.  However, if we want more control with file overrides and error-checking  CL procedures can be used as this example explains.  The first requirement is to create a text file with the FTP statements. The statements can also helpful when entered manually during testing of your FTP automation.

The first FTP statement in this new text member is a log-on with a valid user-id and password.  The sign on is followed by a 'change directory' statement to set up the directory on the remote computer. The FTP directory is similar to a library name on the iSeries. The 'cd ..' sets the directory back one level for the location where the transferred file is to be placed on the remote system. The location can be specified as cd library-name as in "cd pospaylib".
The text file contains:

USER some_user_name fictitious_password
cd.. quote site lrecl=112 cy pri=10 sec=5
put mylib/pospay "acct_company_file(+1)"
put mylib/trailer "acct_company_file_trg(+1)"

The 'quote site' statement defines the record length, block size, etc. on the remote system. CY is short for 'cylinders', PRI and SEC are short for 'primary' and 'secondary' respectively. The 'quote site' in this example is used by a remote mainframe to determine the record length and the number of primary and secondary cylinders for allocating storage space. Your situation may be slightly different and the 'quote' statement may or may not be required for any given FTP solution. The parameters for the 'quote' and 'put' statements will largely depend on requirements from your target system.

The 'bin' statement can optionally be added after the quote statement in order to transfer the file in binary but this also must match the specific requirements of the receiving computer.

The file name in the 'put' statement normally will be the actual name of an existing file on the target system. The target computer might be another iSeries, a mainframe, a Unix or a windows machine. In this example the remote system receives two files, the second being a one record trailer for totals.  The total record has to match or the batch would be rejected at the remote system and an FTP error was issued.

Next we need to create a CL to actually run the FTP.  First let the procedure copy the source statements from the text file into a qtemp file.

CPYF fromfile(mylib/qtxtsrc) tofile(qtemp/ftpsrc) frommbr(ppoftrns) tombr(ppoftrns) mbropt(*replace) crtfile(*yes)

Then override to the new file in qtemp.

OVRDBF file(ppoftrns) tofile(qtemp/ftpsrc) mbr(ppoftrns)

Next both INPUT and OUTPUT files need an override.  The file names INPUT and OUTPUT are intrinsically understood by the FTP command.  With the overrides in place the FTP file INPUT points to the source statements in our text file.  The override to the file named OUTPUT represents a text file where we store the results. 

A review of qgpl/qoutput will show all of the ftp statements including with transmission errors.  In part III we also use the OUTPUT file to check for errors during the transmission.

OVRDBF file(INPUT) tofile(qtemp/ftpsrc) mbr(ppoftrns)
OVRDBF file(OUTPUT) tofile(qgpl/QOUTPUT) mbr(OUTPUT)
CLRPFM file(qgpl/qoutput) mbr(output)

FTP 'nnn.nn.nnn.nn' replace with the actual name or IP address of the target system

The following copy statement will save the actual FTP results  to a separate file.

CPYF fromfile(qgpl/qoutput) tofile(qgpl/ftpout) mbropt(*add) fmtopt(*cvtsrc)

If no serious errors were found during the transmission it is a good idea for the procedure to do a CPYF to a historical recovery file. This file keeps track of every transferred record for safekeeping and auditing purposes. Of course it is best to write the history based on something unique, preferably a batch number and the date (if the FTP runs once daily). Note the from-file is the one specified the first put  statement in the text source member.

CPYF fromfile(pospay) tofile(pospayh) mbropt(*add) crtfile(*no) fmtopt(*NOCHK)

Don't forget to delete your overrides and clear out the temporary files.

CLRPFM file(qgpl/qoutput) mbr(output)
CLRPFM file(*libl/pospay)
DLTF file(qtemp/ftpsrc)

Lastly, if no errors where encountered, send a message that the procedure completed successfully.  The process is not really complete until it is determined if the receiving system received the transfer or if some errors were sent back. proceed to part III - FTP error checking