Processing Several Files with the Same File Specification

Q. I have an RPG program that needs to read a large number of files and perform the same processing for each of them. I may not know the file names at compile time. Some of the files have different record lengths, and some of them even have different formats, although the critical fields correspond in each file (e.g., each file format has a field called Tranamount, Signed(13.2). How can I accomplish this requirement without having to code separate file specifications for each file?

A. You can probably do what you want with a combination of the EXTFILE and USROPN F-spec keywords. If you can identify the the record format with a record identification field, you can describe each format with input specifications. In the file specification, use the record length of the longest record.

Here’s a skeleton of what your code might look like:

FInput     IF   F 1028        Disk    Extfile(Inputfile) 
F                                     Usropn
  // ------------------------------ Standalone variables 
D Inputfile       S             21 
  // ------------------------------ Input specifications 
  //                        Transtype = 22 Savings debit 
IInput     NS  22    1 C2    2 C2 
I                                  1    2  Trantype 
I                                 15   27 2Tranamount 
  //                       Transtype = 23 Checking debit 
I          NS  23    1 C2    2 C3 
I                                  1    2  Trantype 
I                                 35   47 2Tranamount
  //                           Transtype = 24 Misc debit 
I          NS  24    1 C2    2 C4 
I                                  1    2  Trantype 
I                                 63   75 2Tranamount 
  // ---------------------------------------------------
 /Free
   Dou *Inlr;
     // For each file to be processed,
     // assign name to Inputfile.
     // Library name is optional.
     Inputfile = 'MYLIBRARY' + '/' + 'MYFILE';
     Open Input;

     Dou %Eof(Input);
       Read Input;
         If %Eof(Input);
         *Inlr = *On;
       Else;
         // Process record
       Endif;
     Enddo;

     Close Input;
   Enddo;
   Return;

  /End-free