It turns out that there is no simple way of generating a list of files currently residing in a folder on the IBM i IFS. Simple, in this case, would be a command like DSPLNK OUTPUT(*FILE). An API does exist, but it turns out that the same results can be achieved both quickly and simply if you are willing to spend a bit of time in Qshell.
This post is a simplified reworking of an issue I encountered some time ago, but it’s worth documenting.
The issue is that I have a number of files being sent, via FTP, to a folder in the IFS. I need to be able to copy these files into an import file for subsequent processing and then archive them. The problem, of course, is that I don’t know what the file names will be beforehand.
Here’s a solution:
First create an extract file in QTEMP. You will need this in order to compile the program. If you are going to submit the compile in batch, you will need to create the file in some other library:
crtpf file(QTEMP/EXTRACTP) rcdlen(20)
And then the CL program:
pgm
dcl &fromfile *char 50
dcl &tombr *char 50 value('/qsys.lib/LSCLIB.lib/IMPORTP.file/IMPORTP.mbr')
dcl &dltfile *char 100
dcl &cpyfile *char 100
dclf EXTRACTP
/* First, I use QShell to list the files in the Extract folder */
/* The output of this is redirected to ExtractedFiles.txt. */
qsh cmd('ls /home/PAUL/Extract/ > /home/PAUL/ExtractedFiles.txt')
/* In order to use this information, create the extract file in qtemp and */
/* copy the ExtractedFiles.txt stream file into it. */
/* If the file already exists, the MONMSG clears it. */
crtpf file(QTEMP/EXTRACTP) rcdlen(20)
monmsg CPF7302 exec(clrpfm QTEMP/EXTRACTP)
cpyfrmstmf fromstmf('/home/PAUL/ExtractedFiles.txt') +
tombr('/qsys.lib/qtemp.lib/EXTRACTP.file/EXTRACTP.mbr') +
mbropt(*REPLACE)
/* And now I can use QTEMP/EXTRACTP to drive my way through the Extract */
/* folder */
dowhile '1'
rcvf
monmsg msgid(CPF0864) exec(LEAVE)
/* Copy the next stream file to the IMPORTP physical file */
chgvar &fromfile value('/home/PAUL/Extract/' *tcat &EXTRACTP)
cpyfrmstmf fromstmf(&fromfile) tombr(&tombr) mbropt(*add) +
STMFCCSID(819)
/* Copy the stream file to the Archive */
chgvar &cpyfile value('cp /home/PAUL/Extract/' *tcat &EXTRACTP +
*bcat '/home/PAUL/Archive')
qsh cmd(&cpyfile)
/* Then remove the stream file from the Extract folder */
chgvar &dltfile value('rm /home/PAUL/Extract/' *tcat &EXTRACTP)
qsh cmd(&dltfile)
enddo
/* Clean up and exit
qsh cmd('rm /home/PAUL/ExtractedFiles.txt')
dltf qtemp/EXTRACTP
endpgm
It should go without saying that some of the names have been changed and that the above program should be treated as a sample only.
Being able to move information between the QShell/IFS and traditional i on Power environments is both useful and (in my experience) increasingly important. Although it does take a bit of thinking about, it isn’t difficult which is why I find that the oft-seen solution of “buy this tool” is both disappointing and (often) overkill.