MC Press Online

Tuesday, Jun 27th

Last updateTue, 27 Jun 2017 9am

You are here: Home ARTICLES Programming RPG TechTip: Automate Data Transfers Using FTP

Programming / RPG

TechTip: Automate Data Transfers Using FTP

Support MC Press - Visit Our Sponsors

NEW BOOK!

Evolve Your RPG Coding: Move from OPM to ILE ... and Beyond


ORDER YOUR COPY

*******************

Click for this Month's

Bookstore Special Deals

i talk to other servers. No, it's not a typo. This TechTip series will get your System i talking to other servers.

 

Back in the old days, when our server was called AS/400, there were few or even no "satellite systems" like PC servers that needed to pass and/or receive data from it. Nowadays, it's a totally different landscape: the i sits together with a broad scope of servers and devices with which it has to communicate, passing and receiving data. These interfaces are, at times, manual data imports or exports performed by operators via boring and repetitive procedures.

 

This TechTip series suggests a few ways to automate these procedures using FTP, ODBC, and WebSphere MQ. It doesn't intend to set a standard, just to get you thinking about your own IT landscape and interfacing needs. Hopefully, you'll find here the answers to some of your problems regarding i data import/export.

Automate Data Transfers Using FTP

Let's start with the basics: one of the most commonly used procedures to transfer data to and from the System i is FTP. I won't go into detail in this article, but there are some points worth mentioning. First of all, I'll always be referring to FTP started on a Windows PC. This may sound strange, but since Windows is (still) one of the most-used file servers and desktop OSes out there, I think it makes sense.

 

You can start an FTP session via a command line and use it interactively to transfer data. That's part of the boring procedure mentioned earlier. This first TechTip is on how to automate the data transfer. By using a batch file to invoke the FTP command and a script file to perform the data transfer actions, you avoid typing the same thing over and over.

 

FTP has some options to perform batch transfers. Let's start with option –n. This option tells the FTP command not to ask for the user name and password when the FTP session is initiated. Then, in order to run the transfer, you need to use a script file (which will contain the commands to perform) and tell the FTP where it's located. This is done using the option –s:<script file name>. Finally, you have to indicate the target system name. So your batch file should look a bit like this:

 

@echo Transferring file

@echo Off

ftp -n -s:U:ScriptsMyScript.scp MYSystemi

 

U:ScriptsMyScript.scp indicates the name and location of the script, and MySystemi indicates the target system name.

 

Of course, this is usually not that simple. You will probably need to change to the proper folder or rename the file to be transferred (or perform some other actions using DOS commands). You can use entry parameters to your batch file to achieve that. Here's another example:

 

@echo Transferring file %1

@echo Off

H:

CD

rename %1 myfile.txt

ftp -n -s:U:ScriptsMyScript.scp MYSystemi

 

This batch file will change to path H:, rename the entry parameter (%1) file name to myfile.txt, and execute the myscript.scp that resides on U:Scripts on system MySystemi.

 

The use of entry parameters on the batch file is especially useful because you cannot pass parameters into the script file; it's a kind of black box of commands to execute, without any flexibility.

 

Finally, let's see how the script file is composed.

 

Since we're using the –n option, which removes the prompt for user name and password, the first command of any script should always be the login:

 

User username password logs you into your system.

 

Keep in mind that doing this is a security risk: you're storing a user name and password on a text file, somewhere on a file system! You might want to create a user profile for a user who isn't allowed to sign on interactively to your System i and who has limited authorities (for instance, only has access to the IFS folder/library where the user gets or puts the files to be transferred).

 

After the login, you'll have to indicate the commands to be executed. These are standard FTP commands, like cd, lcd, put, get, and so on. Again, keep in mind that the script doesn't have any flexibility, so you have to use fixed directories and IFS folders/libraries on the data transfers. Here's an example of a complete script file:

 

user myuser mypassword

cd /textfiledir

put myfile.txt

quit

 

Note that FTP is case-sensitive, so be sure to type all commands in lowercase. This script, together with the batch file mentioned before, transfers the file indicated when you invoke the batch file to the IFS folder /textfiledir under the name myfile.txt. For example, imagine that your batch file is named mybatch.bat. If you type the following on a command line, you'll be transferring file h:inventory.txt to the IFS folder extfiledir under the name myfile.txt.

 

Mybatch.bat inventory.txt

 

In the second part of this series, we will explore how to automate the transfer of a file received via email on a Lotus Domino mail server.

Rafael Victoria-Pereira

Rafael Victória-Pereira has more than 16 years of IBM i experience, as a programmer, analyst, and manager. Over that period, he has been an active voice in the IBM i community, encouraging and helping programmers transition to ILE and free-format RPG. Rafael has written more than 50 technical articles about topics ranging from interfaces—the topic for his first book Flexible Input, Dazzling Output with IBM i—to modern RPG and SQL, in his popular RPG Academy and SQL 101 series on mcpressonline.com. He writes in an easy-to-read, practical style that's highly popular with his audience of IBM technology professionals.

Rafael currently works as an Enterprise Architect at the Luis Simões Group in Portugal. His areas of expertise include programming in the IBM i native languages (RPG, CL, and DB2 SQL) and "modern" programming languages, such as C# and Python, as well as project management and consultancy.

 

MC Press books written by Rafael Victória-Pereira available now on the MC Press Bookstore.

 

Flexible Input Dazzling Output with IBM i Flexible Input, Dazzling Output with IBM i
Flexible Input, Dazzling Output (FIDO) is all about interfaces. It shares more with a dog than just its name: it has a few tricks of its own!

List Price $71.95
Now On Sale
 

 

Evolve Your RPG Coding Evolve Your RPG Coding
Evolve Your RPG Coding helps you transition to modern RPG programming, guiding you step by step through ILE and free-format RPG, SQL, and modernization techniques.

List Price $79.95
Now On Sale
 
BLOG COMMENTS POWERED BY DISQUS