Automate Your File Transfers Using FTP on the IBM i

  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Create a reusable program to make automating your file transfers easy.


File Transfer Protocol (FTP) is a popular way to get your files from one computer to another over the network. The IBM i makes it easy to do this, and I will show you how to create a reusable program that will make your automation of the process even easier.

Interactive FTP on the IBM i

Prior to automating your FTP process, you should test your file transfer interactively. For this example, we will be uploading an XML file named mcpress.xml onto a fictitious server:


Fictitious Target FTP Server Information

Target FTP Server

User Name





Testing the FTP process interactively will ensure that the IBM i has access to the server and that the user name and password are active on the target server. You can do this by typing FTP or STRTCPFTP on the command line and prompting it to enter the DNS name or IP address of the server, or you can enter the FTP server information with either of these commands:








Once you have successfully connected to the FTP server, you will enter your user name and password interactively. To complete the account verification, you should use the put command to upload a file to make sure you have the authority to do this.


For our example, we will be uploading an XML file from the IFS. I like to ensure that we are using namefmt 1 to indicate the file location in a directory format and specifying asc to perform an ASCII transfer. Here are the commands to execute at the FTP command line to accomplish the task:


namefmt 1




put /Public/mcpress.xml




Upon successful execution of these commands, you will have uploaded the XML file to your target FTP server. You can verify this by signing back onto the FTP server and executing the ls command to see your file on the server. Then type quit to close the connection.


Well, that was pretty useful information. But I am sure you do not want to manually send your files on a regular basis. So let's create a program to do it for us.

Using FTP in Batch

In order to automate the FTP process, we will most likely want to run it in batch. To do this, you can call the STRTCPFTP command, specifying the target FTP server location. When you do this, it will look for a file called INPUT in your library list to execute the FTP commands sequentially.


For our example, we will be creating a new physical file called FTPINPUT. I like to do this in case IBM provides this file during an upgrade; this way, that wouldn't doesn't cause problems with our existing file or delete all of the commands that are compiled as it is being built. To use the FTPINPUT file, we will just override the INPUT file to use our new FTPINPUT file.


The FTPINPUT file will be a physical file containing a single field that is large enough to accommodate most expected commands that you would be executing, with a little extra. I have chosen to set the field size to 2,000 characters, but you can set yours to be any size you feel sufficient. Here's the DDS for the FTPINPUT physical file:


A          R FTPFMT    

A            FTPCMD      2000     


Once you have the FTPINPUT file created with the MCPRESS member, you can enter the commands to be sequentially executed by STRTCPFTP. The first record in the file will contain the user name and password. So, using the FTP information from the table above, the first record of FTPINPUT will be this: 


tomsnyder mcpress


The rest of the records in FTPINPUT will be populated with the contents previously mentioned. A separate record will need to be created for each line.

Automated FTP on the IBM i

Putting in the extra effort at the beginning of implementing your automated FTP will save you a lot of time further down the line, as I have found out for myself. You could just create the FTPINPUT file, override it in a CL, and call the STRTCPFTP command from within a CL, and you'd be done. It sounds simple, and it sounds great, but what happens is that your automated FTP process begins to grow as you support more and more servers. Then you find yourself wanting to do more with your FTP process, which requires capabilities that you would need to use RPG for. At this point, you'd really wish that you had written the program in RPG, which is why we will be creating our program in RPG instead of CL.


To make our FTP program more flexible, we could put our server information into a physical file to be used with our STRTCPFTP command. Chances are that you may be supporting multiple sites for multiple purposes, so we could use these attributes as the composite key in our physical file to retrieve the location of the target FTP server. This would easily accommodate changes in the target FTP server that could support a change of FTP servers, failover, or different phases of development with a test and production server. Here's the DDS for the FTPSERVERS physical file:


A          R FTPSFMT 

A            FTPSITE       10     

A            FTPJOB        10        

A            FTPSVR       512     

A          K FTPSITE              

A          K FTPJOB     


As you start using this program more and more, you will find that you will be happy you set this up in the beginning.


We will be using FTPSERVERS as the name of our physical file, and here are the contents that we will be using for this example:


Contents of FTPSERVERS Physical File









To keep this article within a reasonable size, we will only be using FTPSERVERS to store the location of the target server. Later on, you may want to consider putting the user name and password of the file in the FTPSERVERS file and building the FTPINPUT within the program. Or maybe you will want to encrypt the user name and password. You could store the user name and password as an encrypted value in the FTPSERVERS file, decrypt it in the program, create the FTPINPUT with the decrypted value, and clear the file when you are done--all future possibilities that can be easily implemented using RPG.


OK, we have all of the concepts and files to be used with our program defined. Now we can write the code! Here's the RPG program to perform the FTP upload of the XML file.






DARGS             DS


D ARGSITE                       10A


D ARGJOB                        10A


D* Prototype for QCMDEXC


D ExecuteCommand...


D                 PR                  extPgm('QCMDEXC')


D  argInCommand              65535A   const options(*varsize)


D  argInLength                  15P 5 const




D CMD             S           1024A


D DSPBYTES        S             52A






C     *ENTRY        PLIST


C                   PARM                    ARGS


C     FTPKLIST      CHAIN     FTPSERVERS                         68


C                   IF        *IN68 = *OFF


C                   EVAL      CMD = 'OVRDBF FILE(INPUT)'


C                                 + ' TOFILE(FTPINPUT)'


C                                 + ' MBR(' + %TRIM(ARGJOB) + ')'


C                   CALLP     ExecuteCommand(%trim(CMD):%len(%trim(CMD)))




C                   EVAL      CMD = 'STRTCPFTP'


C                                 + ' RMTSYS('''


C                                 + %trim(FTPSVR) + ''')'


C                   CALLP     ExecuteCommand(%trim(CMD):%len(%trim(CMD)))


C                   ELSE


C                   EVAL      DSPBYTES = 'INVALID PARMS!'


C                                      + ' SITE: ' + %TRIM(ARGSITE)


C                                      + ' JOB: ' + %TRIM(ARGJOB)




C                   ENDIF


C                   EVAL      *INLR = *ON






C                   KFLD                    ARGSITE


C                   KFLD                    ARGJOB


After you change the FTP server settings to the appropriate values for your FTP server, you can run the program with the key information for the FTPSERVER file:




A successful FTP transfer will result in something similar to this:



Enter an FTP subcommand.

   > namefmt 1

   500 'SITE NAMEFMT 1': command not understood

   Client NAMEFMT is 1.

   Enter an FTP subcommand.

   > asc

   200 Type set to A.

   Enter an FTP subcommand.

   > put /Public/mcpress.xml

   227 Entering Passive Mode (12,345,678,90,12,345).

   125 Data connection already open; Transfer starting.

   226 Transfer complete.

    10127 bytes transferred in 0.023 seconds. Transfer rate 432.085 KB/sec.

   Enter an FTP subcommand.

   > quit


   Press ENTER to end terminal session.


Download the Code

You can download the code used in this article--as well as the fixed-format version--by clicking here.


Uploading Physical File Members

If you wanted to use FTP to send a physical file member to the target server, you could use NAMEFMT 0 or 1. If you were to upload a physical file member of MCPRESS from a file named MYFILE that resides in a library named MYLIB, you could use either of the following naming formats: 



Physical File Member

0 (Library File Naming Format)


1 (Common FTP Naming Format)




The following commands are two different ways to upload the same physical file member with different name formats:


Upload the file using NAMEFMT 0:




Upload the file using NAMEFMT 1:




More Information


For more information on FTP on the IBM i, you can go to the IBM Web site and read all about it.