The EXTFILE keyword specifies which file, in which library, is opened.
filename can be a literal or a variable. You can specify the value in any of the following forms:
filename
libname/filename
*LIBL/filename
Special value *EXTDESC can be used to indicate that the parameter for the EXTDESC keyword should also be used for the EXTFILE keyword.
If you have specified an override for the file that RPG will open, that override will be in effect. In the following code, for the file named INPUT within the RPG program, the file that is opened at runtime depends on the value of the filename field.
Finput if f 10 disk extfile(filename)
If the filename field has the value MYLIB/MYFILE at runtime, RPG will open the file MYLIB/MYFILE. If the command OVRDBF MYFILE OTHERLIB/OTHERFILE has been used, the actual file opened will be OTHERLIB/OTHERFILE. Note that any overrides for the name INPUT will be ignored, since INPUT is only the name used within the RPG source member.
* The name of the file is known at compile time
Ffile1 IF F 10 DISK EXTFILE('MYLIB/FILE1')
Ffile2 IF F 10 DISK EXTFILE('FILE2')
* The name of the file is in a variable which is
* in the correct form when the program starts.
* Variable "filename3" must have a value such as
* 'MYLIB/MYFILE' or 'MYFILE' when the file is
* opened during the initialization phase of the
* RPG program.
Ffile3 IF F 10 DISK EXTFILE(filename3)
* The library and file names are in two separate variables
* The USROPN keyword must be used, so that the "filename4"
* variable can be set correctly before the file is opened.
Ffile4 IF F 10 DISK EXTFILE(filename4)
F USROPN
D filename4 S 21A
* EXTFILE variable "filename4" is set to the concatenated
* values of the "libnam" and "filnam" variables, to form
* a value in the form "LIBRARY/FILE".
C EVAL filename4 = %trim(libnam) + '/' + filnam
C OPEN file4
* At compile time, file MYLIB/MYFILE5 will be used to
* get the external definition for the file "file5",
* due to the EXTDESC keyword.
* At runtime, the file MYLIB/MYFILE5 will be opened,
* due to the EXTFILE(*EXTDESC) keyword.
Ffile5 if e DISK
F EXTFILE(*EXTDESC)
F EXTDESC('MYLIB/MYFILE5')