CoolSpools FTP Commands

While the CoolSpools application is best known for its powerful IBM i spooled file and database conversion commands, it also includes multiple commands to simplify FTP file processing on your IBM i server. The CoolSpools conversion commands all allow you to specify parameter TOSTMF(*FTP) to deliver the converted document to a network file server using FTP, but there are also a number of standalone FTP commands.

The standalone FTP commands listed below are included in the CoolSpools V7R1 base application, so will be available if you have installed any CoolSpools module. Some of these FTP commands may require an update to the latest CoolSpools V7R1 fix pack (FTPDLT and FTPGET require fix pack 056 or above, FTPDIR requires fix pack 078 or above). The FTP commands all support open FTP connections on port 22, secure FTP connections on port 990, or the use of a custom non-standard port number.

Sample code provided below also includes the command IMPFXDDBF, which is part if the CoolSpools Database module, so will only be available on your system if you have installed the CoolSpools Database module or the CoolSpools Suite.

FTPPUT Put file to a remote file server

This command allows you to send a file from your IBM i server to a remote file server.

FTPPUT LCLPATH('/localpath/file.csv') RMTSYS(REMOTESERVER)
       RMTPATH('/remotepath/file.csv') RMTUSER(REMOTEUSER)
       RMTPWD(REMOTEPWD)

FTPGET Get file from a remote file server

This command allows you to get a file from a remote file server, typically to a folder on your IBM i server’s IFS.

FTPGET RMTPATH('/remotepath/file.csv') RMTSYS(REMOTESERVER)
       LCLPATH('/localpath/file.csv') RMTUSER(REMOTEUSER)
       RMTPWD(REMOTEPWD)

FTPDLT Delete file from a remote file server

This command allows you to delete a file from a remote file server, so long as you have permission to do so.

FTPDLT RMTPATH('/remotepath/file.csv') RMTSYS(REMOTESERVER)
       RMTUSER(REMOTEUSER) RMTPWD(REMOTEPWD)

FTPDIR Generate list of files on a remote file server

This command generates a directory listing of a folder on a remote file server. The results are placed into an IBM i database table using a standard file format.

FTPDIR RMTPATH('/remotepath/*.csv') TOFILE(QTEMP/FTPDIROUT)
       CRTFILE(*YES) RMTSYS(REMOTESERVER)
       RMTUSER(REMOTEUSER) RMTPWD(REMOTEPWD)

The file that is created contains the following fields:

Field Attributes Text
SARMTSYS CHAR(32) Remote System
SARMTPATH CHAR(256) Remote Path
SAFILE CHAR(256) File Name
SASIZE PACKED(15,0) File Size (Bytes)
SADATE DATE File Date

Using The CoolSpools FTP Commands in Combination

You can achieve powerful results very simply by combining these FTP commands. For example, the sample CL program below will look for new order files named *.ord on a remote file server, copying them to a folder on the IBM i server’s IFS and then importing the file content to order header and detail database tables, before finally deleting the order file from the remote file server.

0001.00              PGM
0002.00
0003.00           /* Declare variables */
0004.00              DCL        VAR(&RMTSERVER) TYPE(*CHAR) LEN(80) VALUE('ServerName')
0005.00              DCL        VAR(&RMTUSER) TYPE(*CHAR) LEN(20) VALUE('UserName')
0006.00              DCL        VAR(&RMTPWD) TYPE(*CHAR) LEN(20) VALUE('Password')
0007.00              DCL        VAR(&RMTPATH) TYPE(*CHAR) LEN(1024)
0008.00              DCL        VAR(&LCLPATH) TYPE(*CHAR) LEN(1024)
0009.00
0010.00           /* Standard file format for FTPDIR results (we will override later) */
0011.00              DCLF       FILE(COOLSPV7R1/CS_FTPDIR)
0012.00
0013.00           /* Delete temporary work file from QTEMP if it already exists */
0014.00              DLTF       FILE(QTEMP/WRK_DIROUT)
0015.00              MONMSG     MSGID(CPF0000)
0016.00
0017.00           /* Run FTPDIR to get a list of new order files on remote server */
0018.00              FTPDIR     RMTPATH('/orders/*.ord') +
0019.00                           TOFILE(QTEMP/WRK_DIROUT) CRTFILE(*YES) +
0020.00                           RMTSYS(&RMTSERVER) RMTUSER(&RMTUSER) +
0021.00                           RMTPWD(&RMTPWD)
0022.00
0023.00           /* Override CS_FTPDIR to the work file we just created in QTEMP */
0024.00              OVRDBF     FILE(CS_FTPDIR) TOFILE(QTEMP/WRK_DIROUT) +
0025.00                           MBR(*FIRST) OVRSCOPE(*JOB) SHARE(*YES) +
0026.00                           OPNSCOPE(*JOB)
0027.00
0028.00           /* Loop through the list of remote order files */
0029.00  LOOP:       RCVF
0030.00              MONMSG     MSGID(CPF0000) EXEC(GOTO CMDLBL(ENDLOOP))
0031.00
0032.00           /* Ignore remote order files with zero size */
0033.00              IF         COND(&SASIZE *EQ 0) THEN(GOTO CMDLBL(LOOP))
0034.00
0035.00           /* Run FTPGET to get the file from the remote server to the IFS */
0036.00              CHGVAR     VAR(&RMTPATH) VALUE(&SARMTPATH *TCAT &SAFILE)
0037.00              CHGVAR     VAR(&LCLPATH) VALUE('/sales/orders/' *TCAT &SAFILE)
0038.00              FTPGET     RMTPATH(&RMTPATH) RMTSYS(&RMTSERVER) +
0039.00                           LCLPATH(&LCLPATH) RMTUSER(&RMTUSER) +
0040.00                           RMTPWD(&RMTPWD)
0041.00
0042.00           /* Load the file content to database tables with IMPFXDDBF */
0043.00              IMPFXDDBF  FROMSTMF(&LCLPATH) TOFILE(ORDHDR) +
0044.00                           MBROPT(*ADD) COLUMNS((1 2 *SLTONLY *NONE +
0045.00                           RCDTYP) (*NEXT 6) (*NEXT 5) (*NEXT 30) +
0046.00                           (*NEXT 10)) SLTROW((RCDTYP *EQ '02'))
0047.00              IMPFXDDBF  FROMSTMF(&LCLPATH) TOFILE(ORDLIN) +
0048.00                           MBROPT(*REPLACE) COLUMNS((1 2 *SLTONLY +
0049.00                           *NONE RCDTYP) (*NEXT 6) (*NEXT 3) (*NEXT +
0050.00                           5) (*NEXT 20) (*NEXT 5) (*NEXT 7)) +
0051.00                           SLTROW((RCDTYP *EQ '03'))
0052.00
0053.00           /* Run FTPDLT to delete the order file from remote server */
0054.00              FTPDLT     RMTPATH(&RMTPATH) RMTSYS(&RMTSERVER) +
0055.00                           RMTUSER(&RMTUSER) RMTPWD(&RMTPWD)
0056.00
0057.00           /* Return to the start of the loop to process next order file */
0058.00              GOTO       CMDLBL(LOOP)
0059.00
0060.00
0061.00           /* No more order files – clear up override and temporary work file */ 
0062.00  ENDLOOP:    DLTOVR     FILE(CS_FTPDIR) LVL(*JOB)
0063.00              DLTF       FILE(QTEMP/WRK_DIROUT)
0064.00
0065.00              ENDPGM