In previous posts I have written about using FTP to copy files from one IBM i partition to another. Almost all of the examples I gave were for transferring of a file, or files, in what I call the "IBM i environment", good old libraries and files.

Earlier this week one of my colleagues came to me and asked if there was a way to use FTP to copy a file in the IFS of one IBM i to the IFS of another partition. I looked back at the posts I had written and realized that I did not have a good example of how to do this. I thought I would share what we played with so he could understand what to do.

Before we get started I need to explain that FTP with IBM i has two naming conventions for the file's path:

Library name format: which is the familiar library/file.member

Example: MYLIB/MYFILE.MEMBER1

Hierarchical file system: this file naming convention is used for all files in the IBM i those in the IFS and those in the "IBM i environment"

Example 1: /folder/subfolder/file.txt

Example 2: /qsys.lib/mylib.lib/file.file/member.mbr

When using FTP it is possible to switch from one naming format to another by using FTP's NAMEFMT subcommand.

This changes the FTP session to use the library name format:

namefmt 0

And this changes the FTP session to use the Hierarchical file system:

namefmt 1

I will show where this subcommand should be used in the examples below.

The scenarios we played with were:

In these examples I am not going to use the IP addresses of the IBM i partitions, I am going to use their network names. I will be using:

THISSYS will be the IBM i partition I am currently on

will be the IBM i partition I am currently on OTHERSYS will be the IBM i partition I want to send data to

Both of these IBM i need to have their FTP servers started. I can do that using the following command:

STRTCPSVR SERVER(*FTP)

But, before starting the FTP server on the IBM i it is best to see if any FTP servers have are already active. I can do that using the following command:

WRKACTJOB JOB(QTFTP*)

If any jobs are displayed then I know that FTP has already been started.

And, finally, onto the examples!

Copy file from the IFS of one IBM i partition to another

I start a FTP session using the following command at any command line:

ftp othersys

I could use the IP of OTHERSYS instead, but why make it more complicated that this needs to be.

Something similar to below will be shown.

File Transfer Protocol Previous FTP subcommands and messages: Connecting to host OTHERSYS at address 999.999.99.99 using port 21. 220-QTCP at OTHERSYS. 220 Connection will close if idle more than 5 minutes. Enter login ID (simon): ===> F3=Exit F6=Print F9=Retrieve F17=Top F18=Bottom F21=CL command line

First I need to signon with a user profile and password that is valid on OTHERSYS .

Having signed on I need to change the name format to the Hierarchical file system by entering the following:

namefmt 1

I get the following response to show that this was accepted by both sides of the FTP connection:

> namefmt 1 250 Now using naming format "1". Server NAMEFMT is 1. Client NAMEFMT is 1.

Now I can type in the put subcommand, which will "put" the file in the other IBM i's IFS.

put /MyFolder/test.txt /OtherFolder/test.txt

This will copy the file text.txt from the IFS folder MyFolder from to the folder OtherFolder on OTHERSYS .

I get the following response to show that file was copied successfully:

> put /MyFolder/test.txt /OtherFolder/test.txt 227 Entering Passive Mode (99,999,9,99,999,999). 150 Sending file to /OtherFolder/test.txt 226 File transfer completed successfully.

Copy file from the IFS to the "IBM i environment" in the same partition

This is an example of discovering something through "play", as I had no idea I could FTP file from the IFS into an "IBM i environment" file in the same partition.

I am going to FTP the file test.txt in MyFolder to my library. I do not have to create a file in my library before hand, FTP will create one for me. Be warned if a file of that name already exists the data from the copied file will be added to it. The result of the FTP copy is what I would call a flat file.

This time I start FTP with the name of this IBM i:

ftp thissys

Once I have signon to the server I need to issue the namefmt subcommand again.

namefmt 1

Now I can enter the put subcommand:

put /MyFolder/test.txt /QSYS.LIB/MYLIB.LIB/TESTTEST.FILE/TESTTEST.MBR

The from location is the path name to the file in the IFS.

The "to" location looks a lot more complicated. I entered in upper case, but this will work in lower or mixed case too. This is made up of…

QSYS.LIB : QSYS library is like to root for the IBM i environment, all other libraries exist within it. This has to start the location if you are using the "IBM i environment".

: QSYS library is like to root for the IBM i environment, all other libraries exist within it. This has to start the location if you are using the "IBM i environment". MYLIB.LIB : This is the name of the library I want the file to be placed in. As this is a library the extension is .LIB

: This is the name of the library I want the file to be placed in. As this is a library the extension is TESTTEST.FILE : Name of the file to which the data will be copied. As it is a file it has to have the extension .FILE

: Name of the file to which the data will be copied. As it is a file it has to have the extension TESTTEST.MBR : Specifying the member is optional. I only did it in this example to show that it is possible. As this is the member it has to have extension .MBR

And I receive the response that the file was copied into my file in my library.

> put /MyFolder/test.txt /QSYS.LIB/MYLIB.LIB/TESTTEST.FILE/ TESTTEST.MBR 227 Entering Passive Mode (99,999,9,99,999,999). 150-NAMEFMT set to 1. 150 Sending file to member TESTTEST in file TESTTEST in library MYLIB. 226 File transfer completed successfully.

Copy file from the "IBM i environment" to the IFS in the same partition

How about the other way? From a "IBM i environment" file to an IFS file:

If i am not already using name format 1 I need to change the naming format in my FTP session.

namefmt 1

The put subcommand is just the other way around to the previous put I showed.

put /qsys.lib/mylib.lib/testtest.file /MyFolder/test2.txt

This shows you can enter the "IBM i environment" path in lower case, and as the file has only one member I do not have to give the member name. The response is:

> put /qsys.lib/mylib.lib/testtest.file/testtest.mbr /MyFolder/ test2.txt 227 Entering Passive Mode (99,999,9,99,999,999). 150 Sending file to /MyFolder/test2.txt 226 File transfer completed successfully.

Copy file from the IFS to Microsoft Windows server

The final example is sending the file from the IFS to a Windows FTP server. This example is a bit of a cheat as when I login to it the default folder is the one I want to use.

This time I start FTP with the IP address of the Windows FTP server:

ftp '999.99.999.9'

I login with my user id and password for the Windows FTP server.

I need to change the name format again.

namefmt 1

As I am connected to a Windows FTP server the responses are slightly different.

namefmt 1 500 'SITE NAMEFMT 1': command not understood Client NAMEFMT is 1.

The 500 error is because the Windows FTP server does not understand the namefmt subcommand, which isn't a problem.

Next is the put subcommand. Notice that I have not given a "to" location as I want the file in my default directory/folder on the Windows FTP server

put /MyFolder/test.txt

The responses from the Windows server are slightly different from those sent by the IBM i:

put /MyFolder/test.txt 227 Entering Passive Mode (999,99,999,9,99,999) 125 Data connection already open; Transfer starting. 226 Transfer complete.

How can I tell if my file was sent to the Windows FTP server? I can use the ls FTP command, which list the contents of the current directory/folder.

ls

I can see that the file, test.txt , is now on the Windows FTP server.

ls 227 Entering Passive Mode (999,99,999,9,99,999) 125 Data connection already open; Transfer starting. test.txt

This article was written for IBM i 7.3, and should work for some earlier releases too.