Dynamic FTP scripts

How to update ftp source scripts from a CL

Often FTP scripts are kept as static source members that are generally used as the FTP INPUT file. However, occasionally the script needs to be updated at run time and a static source member won't do. To overcome this limitation it is fairly easy, using an SQL statement, to update the FTP source member dynamically.

In this example the file recipient is requiring that the file name contain a time stamp. There is a CL program and the FTP source member which is stored separately in QTXTSRC. This is only an example so no actual sign-on information is used.

/* set up variables */

DCL        VAR(&STMT ) TYPE(*CHAR)  LEN(200)
DCL        VAR(&TODAY) TYPE(*CHAR)  LEN(6)
DCL        VAR(&TIME)  TYPE(*CHAR)  LEN(6)
DCL        VAR(&FILNAM) TYPE(*CHAR) LEN(50)
DCL        VAR(&TOSYS  ) TYPE(*CHAR) LEN(50)  INZ('nn.nn.n.nn') substitute with target IP address



/* assume target wants time stamp date in filename_YYMMDDHHMMSS.txt format */

     CVTDAT    DATE(&TODAY) TOVAR(&DATYYMD) FROMFMT(*MDY) TOFMT(*YMD) TOSEP(*SYSVAL)



/* first run whatever program generates the file to be FTP'ed */

         CLRPFM FILE(EXTRACT)
         MONMSG MSGID(CPF0000)
         CALL PGM(MYPGM) 
...
/* generate new file name */

       CHGVAR     VAR(&FILNAM) VALUE('here_it_is_' *TCAT +
                            &DATYYMD *TCAT &TIME *TCAT '.TXT')

/* next do the overrides */

       OVRDBF     FILE(QTXTSRC) TOFILE(MYLIB/QTXTSRC) +
                          MBR(FTPSRC1)
       OVRDBF     FILE(INPUT) TOFILE(MYLIB/QTXTSRC) MBR(FTPSRC1)


 /* update the ftp source with new file name  */

       CHGVAR     VAR(&STMT) VALUE('UPDATE MYLIB/QTXTSRC +
                        SET SRCDTA = ''PUT EXTRACT ' +
                        *CAT &FILNAM *TCAT ''' +
                        WHERE SUBSTR(SRCDTA,1,11) = ''PUT EXTRACT'' +
                        ')

       RUNSQL     REQUEST(&STMT)  (*see note on RUNSQL below)
       OVRDBF     FILE(OUTPUT) TOFILE(QTEMP/CUPFTPLOG) +
                             MBR(CUPFTPLOG)

       STRTCPFTP  RMTSYS(&TOSYS)

       CPYF       FROMFILE(OUTPUT) TOFILE(*PRINT)

       DLTOVR     FILE(*ALL)


Example FTP script source member "FTPSRC1" - do not include the notes in parenthesis :

systemi\consult1  PassW0rD (the sign-on is not interactive)
ascii
cd flr1
lcd mylib
PUT EXTRACT here_it_is_20120215121238.TXT (this line will be updated every time the procedure runs)
quit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Obviously any future maintenance should not remove or alter the "PUT EXTRACT" portion of that source statement.

*If your system does not have the RUNSQL command you can learn how to easily create it here: http://www.as400pro.com/tipView.php?cat=SQL&key=159