So here’s the situation: Five warehouses populating five sets of files (same filenames, different libraries) and I have an RPGLE program that needs to read through each of these to accumulate dispatch information.
The traditional way of doing this would involve writing a CL program to OVRDBF
to each file before calling the RPGLE program. This works, but it’s a bit messy and this can cause future maintenance problems. It’s much better, therefore, to specify the file (and library) to open within the RPGLE program itself. You can do this with the EXTFILE
keyword.
Here’s an example:
* ---------------------------------------------------------------------- * * Program : LSX001R * * Description : Dynamically select which file to open * * ---------------------------------------------------------------------- * h main(Main) fMOVEMENTS if e k DISK usropn extfile(filename) d Main pr extpgm('LSX001R') d library 10a dfilename s 21a * ------------------------------------------------------------------------ * Main Procedure * ------------------------------------------------------------------------ p Main b d Main pi d library 10a /free filename = %trim(library) + '/' + 'MOVEMENTS'; open MOVEMENTS; // Do whatever processing on the file needs to be done... close MOVEMENTS; /end-free p Main e
Hopefully this is reasonably straightforward. In the file spec for the MOVEMENTS file, I have used EXTFILE
with a variable filename
to specify the actual file opened. I have also used the USROPN
keyword as I need to populate the filename
variable before I attempt to open the file.
Populating this field is pretty simple. I pass the library name to the program as a parameter and the first thing the Main procedure does is concatenate the library and file with a ‘/’ separator.
Now I can open the correct file, do whatever processing is necessary, and then close the file afterwards.
A similar approach, using the EXTMBR
keyword can also be used to open a specific file member. You can, of course, combine EXTFILE and EXTMBR in order to dynamically determine both the file and member, if you really want to.
It should be noted that this will only work if you are using traditional database IO. Any embedded SQL will remain unaffected by anything you do in the F-Spec. If you want to do something similar with SQL, you will need to look into the CREATE ALIAS
statement.