Q: There is a file that's placed into a directory in my IFS on a daily basis. Every day the file has a different name! How can I find out the file's name so that I can process it?
A: There are a few different ways to do it.
The easiest way is to use QShell to list the names of files in your directory to a PF that you can read from your CL program. Here's a sample program that does that:
PGM
DCL VAR(&FILELIB) TYPE(*CHAR) LEN(10) VALUE('MYLIB')
DCL VAR(&CMD) TYPE(*CHAR) LEN(500)
DCL VAR(&IFSPATH) TYPE(*CHAR) LEN(50) VALUE('/path/with/files')
DCLF FILE(IFSDIR)
/* CREATE A FILE TO PUT LIST IN. NOTE THAT THIS CANNOT BE +
IN QTEMP, SINCE QSHELL CANNOT ACCESS QTEMP */
CHKOBJ OBJ(&FILELIB/IFSDIR) OBJTYPE(*FILE)
MONMSG MSGID(CPF9801) EXEC(DO)
CRTPF FILE(&FILELIB/IFSDIR) RCDLEN(1000)
ENDDO
CLRPFM FILE(&FILELIB/IFSDIR)
/* LIST ALL *.CSV INTO THE IFSDIR FILE */
CHGVAR VAR(&CMD) VALUE('cd' *BCAT &IFSPATH *BCAT '&&' +
*BCAT 'ls *.CSV >' *BCAT '/qsys.lib/' *CAT &FILELIB +
*TCAT '.lib/ifsdir.file/ifsdir.mbr')
STRQSH CMD(&CMD)
/* Read filenames */
LOOP: RCVF
MONMSG MSGID(CPF0864) EXEC(RETURN)
/* &IFSDIR NOW CONTAINS THE FILENAME OF ONE CSV FILE IN THE +
DIRECTORY */
GOTO LOOP
ENDPGM
Another QShell method would be to execute a program of your choice with the FIND command. The following command will run a new copy of "mypgm" in library "mylib" for each file that it finds. It will pass the filename as a parameter to that program:
STRQSH CMD('find /path/to/files -name *.csv -exec
/qsys.lib/mylib.lib/mypgm.pgm {} \;')
Another method that does not involve QShell would be to write an ILE RPG or ILE C program that reads the filenames using the IFS APIs. This method would probably run faster, but is a bit more complex.
There are examples of this in my RPG IV IFS Tutorial, which you can read online at http://www.scottklement.com/rpg/ifs.html [1] .
Finally, there's a pre-written "DIR" program that you can download from the 'net that lets you list the files in a location of the IFS to *OUTFILE. I've never tried it, but it might work for you. You can find it here: http://www.ediconsulting.com/download.html [2]
Links:
[1] http://www.scottklement.com/rpg/ifs.html
[2] http://www.ediconsulting.com/download.html