FTP Send File

Article ID: 57034

Making FTP Simple(r) with RPG IV

There's little question that FTP is taking over the built-in ability for IBM "i" to send objects between systems. Using FTP gives us one interface to send files and other objects between different platforms (by "platforms" I mean operating systems) as well as between two System i boxes. With FTP, simply provide the remote IP address, the file you want to send, a few optional options and--bam!--it gets sent.

The downside of FTP is that you have to know a few FTP commands, which it isn't difficult unless you haven't used FTP before. It is often something many RPG coders haven't learned until they get told to "FTP this file over to server XYZ.ABC.RST.WXY now."

To make things easier for RPG coders who may be familiar with CL commands, I wanted to create a simple FTP command that allows you to incorporate an FTP transfer within your own CL applications or directly from the Command Entry Display. An FTP expert might (and rightfully so) argue that it's easier to use straight FTP scripts rather than something else to send FTP files. Well, if that were always true, they wouldn't be using things like the ever-popular FileZilla to drag and drop files via FTP between virtually any operating systems. In fact, I even use FileZilla to upload and download savefiles and source files between Windows or Mac OS X and IBM "i".

As an RPG coder, what I want is a command that allows me to say "Send this file using FTP. Here are the IP address, user ID, and password. Now, go do it." But no such CL command existed at the time. So I wrote a simple command named FTP Send File (FTPSNDFILE) that allows you to do just that--send a file using FTP from Command Entry or within CL. This command was originally written in 2005, but I've since revised it and describe it along with its modifications below.

Sending a File Using FTPSNDFILE

The FTPSNDFILE command has a long list of parameters that have good defaults, and it allows you to send any file directly through FTP without manually starting an interactive FTP session or writing an FTP script. The FTPSNDFILE command builds the FTP script on the fly and then runs that script by calling the FTP command.

You can specify a remote system IP address or domain, the file and member you want to send, and the target location. As is usually the case with CL commands that I write, I've selected a good set of default values for the parameters so that you only have to know the IP address, file name, user profile, and password. The rest of the parameters can be left to default. Here's a description of those parameters.

RMTSYS--Remote system. Specify the IP address or domain name of the system that will receive the file you are sending.

FILE--Local file name. Specify the qualified name of the file that you want to send using FTP. A value of *LIBL may be specified for the file's library. However, the FTPSNDFILE command's processing program will convert *LIBL into the actual library name at runtime.

MBR--Local member name. Specify the name of the member that you want to send. The default member name is *FIRST, but you may specify any member name you want or one of the special values, including: *FILE, *FIRST *LAST, or *ALL. In addition, you may specify a generic member name.

TOFILE--Remote file name. Specify the name of the file on the remote system that will receive the data from the file you are sending. The file should already exist so that the external description is preserved. If the file does not exist, the FTP server will create the file for you, but you won't like the results (a flat file). If sending a *SAVF (Save File) make sure the save file exists on the remote location or you'll need to copy the file to a save file once it has been FTP'd.

  • *FROMFILE--Use this when the remote file name should be the same as the local file name as specified on the FILE parameter.

TOMBR--Remote member name. Specify the name of the member into which the data is stored. You may specify either a member name or one of the following special values:

  • *FROMMBR--Use this when the remote member name should be the same as the local member name as specified on the MBR parameter. This value must be specified when a generic local member name is specified or when *ALL is specified for the local member name (MBR) parameter.
  • *TOFILE--The member name is the same as the remote file name specified on the TOFILE parameter.

REPLACE--Replace remote member's data. This parameter adds the "(Replace" parameter to the FTP PUT command. Yes, FTP uses "(Replace" to indicate REPLACE(*YES). However, this parameter has no impact on the results when System i-to-System i transfers are being performed. On the System i, the target file/member's data is always replaced.

RPG TnT: 101 Dynamic Tips ‘n Techniques with RPG IV  by Bob Cozzi
The latest book from author Bob Cozzi contains 300 pages with 101 example RPG IV Tips and Techniques for everyday programming tasks, from date calculations, to regular express searches, to using APIs. Cozzi wrote down every cool technique he's found over the years, updated them, and consolidated them into this compact book that is a great desktop companion, and it includes full example source code. Order your copy today.

USER--Remote FTP user profile. Specify the user profile of the user to sign on to the remote system.

PWD--Remote FTP user's password. Specify the password for the remote user. Note that this parameter's value is not recorded in the job log and must be entered each time you run FTPSNDFILE from Command Entry. From within a CL program, the password can be stored and passed to the FTPSNDFILE command within a CL variable.

MODE--Transfer mode. Specify the kind of transfer to be performed. The valid choices are as follows:

  • *BINARY--The transfer mode is IMAGE/BINARY. This is recommended for System i objects such as database files.
  • *ASCII--The transfer mode is plain ASCII text. This transfer mode is valid for non-database files, such as source file members.

SRCFILE--The name of the source that will receive the generated FTP script. This source file should be as long as possible but at least 152 bytes in length (140 bytes for the source line and the usual 12 bytes for the source sequence and change dates area).

If this source file does not exist, it will be created with a record length of 152 bytes. The default source file name is as follows: Source file QFTPSRC, Library QTEMP.

SRCMBR--The FTP Script source member name. This is the name of the member into which the FTP script is generated. Specify any valid source member name, *FROMMBR, or *GEN. If the member exists, it is cleared; if it does not exist, it is added. The two special values for this parameter are as follows:

  • *FROMMBR--The name specified on the MBR parameter is used as the member name for the FTP script. This member is added to the source file specified on the SRCFILE parameter.
  • *GEN--A member name using the following pattern is automatically generated: FSyyyymmdd, where FS is a constant, and YYYYMMDD is today's system name in YMD format.

LOG--FTP message logging file. Specify the name of a source file that will receive the messages generated by the FTP command. Optionally, specify that the messages are to be delivered as they normally are, via the STDOUT (standard output) device. This parameter has the following special values:

  • *STDOUT (or *STDIO)--Indicates that the FTP log is written to the standard output device, which scrolls up the 5250 screen, similar to an old teletype interface.
  • *NONE--The FTP log is not displayed.

Unlike the SRCFILE parameter, the file specified on the LOG parameter must exist because the FTPSNDFILE command will not create it. You may, however, use the same file name as the SRCFILE parameter, in which case the file is created due to its being specified on the SRCFILE parameter.

LOGMBR--The member name where the FTP log is saved. The following special values may be specified instead of a specific member name.

  • *FROMMBR--The name specified on the MBR parameter is used as the member name for the FTP script. This member is added to the source file specified on the SRCFILE parameter.
  • *SCRIPT--The Script source file member name is used as the LOG file's member name. The member from the SRCMBR parameter is used.
  • *SRCMBR -- Same as *SCRIPT

DSPLOG--Display the FTP log. If *YES is specified, the FTP log is displayed when the FTP transfer completes; if *NO, the FTP log is not displayed.


Learn More Now

The source code for the FTPSNDFILE command is featured in this week's second article. Click through to it and read about how the FTPSNDFILE command works, and how to use the cool helptext Panel Group that a user created for us.

 

Bob Cozzi is the host of RPG World Live, a live, weekly, 2-hour show on System i developments. It is aired on Fridays from 10:00 AM to Noon, Central time on ustream.tv and on RPGWorld.com. Bob also produces RPG World, the most popular RPG IV developer conference of the year. Visit RPGWorld.com for details on the dates and the location of his 2009 RPG World Conference.

ProVIP Sponsors

ProVIP Sponsors