Pages

Wednesday, April 3, 2019

Using FTP with IFS files

ftp with files in ifs

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:

  1. Library name format: which is the familiar library/file.member
    Example:  MYLIB/MYFILE.MEMBER1
     
  2. 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:

  1. Copy file from the IFS of one IBM i partition to another
  2. Copy file from the IFS to the "IBM i environment" in the same partition
  3. Copy file from the "IBM i environment" to the IFS in the same partition
  4. Copy file from the IFS to Microsoft Windows server

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
  • 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".
  • 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
  • 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
  • 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.

2 comments:

  1. To avoid the ugly 500 error message when issuing the "namefmt 1" (or "na 1" for short) against a non-i-FTP-server, use "locsite" in front of it.

    > locsite na 1
    Client NAMEFMT ist 1.

    vice versa, when using a non-i-client to communicate with an i, use "quote site". So even when the client doesn't know anything about "site", it sends it.

    ftp> quote site na 1
    250 Now using naming format "1".
    ftp>

    ReplyDelete
  2. The quote site command is also useful when ftp'ing into an IBM i from another platform. It will allow you to pass the namefmt command on to the IBM i. This way regardless of how the ftp attributes are set on the IBM i you can manipulate files in the IFS from a windows or linux machine.

    ReplyDelete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.