Problem solve Get help with specific problems with your technologies, process and projects.

Automate programming tasks

Learn how to create programming tools that you can use to create sections of code.

One thing I do to make my job easier is create programming tools that I can use to create sections of code for me.

How this works is I have a specific pattern in mind that I need -- say Move's into fields, clearing ALL the fields in a file via Move's or Eval's, or simply inserting a key structure so I can chain to a file. Whatever the mundane programming task, if I can read a file and create the structures or the code automatically, I do it. And that's because, as you know by now, I'm LAZY. But also the results are fantastic. And for files with a large number of fields in them, where you NEED to move data into the fields, this is necessary to get your job done.

I always add to my collection of these tools. In ALL instances, I give my tool the name of the file, and the program responds by writing code that I would use for that particular file. I've been adding to this program for years, so I have sections of this tool that write chunks of Web sites now. But it all started when someone simply said, "Hey, I can't find the source for these files!" So I created the beginnings of my toolbox.

Writing to a Source Physical File
Let's go over the basics of this first. We're writing into a source physical file with the following configuration;

Start   End  K#  Field Name      Len    
    1     6      SRCSEQ      S     6 2  
    7    12      SRCDAT      S     6 0  
   13   114      SRCDTA          102    

Looking at the above, we have a sequence, which because SEU is SUCH a forgiving program, can be filled in with ONE number for all your lines, or add 10 for each sequence in the file you're writing -- It's your choice. You have a date for EACH line number and the code, which will become your new RPG program. One thing I REALLY like doing is creating a list of ALL the KEYS from a file that are available for me. Let's focus on this tool, but you can do MANY variations on this theme. Then I use the following to access the source physical file I'm writing to.

The following CL program is the bread and butter of my program tool. I like calling CR8CSPEC -- or Create C-Specs -- but you can TRULY create ANYTHING you'd like.

Source for CR8CSPEC CL program

What the program does is create the "juice" that my RPG program creation tools will use to output code. Now there's NOTHING sophisticated about this from this point onwards; this is a typical "read a file, and output another file" type of program.

Here's MY program HASTILY WRITTEN to output the C specs for the K lists I have had to cut and paste into thousands of programs over the years.


There's NOTHING sophisticated about this program. It's READING the output from the following command from the calling CL program:

             DSPFD      FILE(*LIBL/&FILE) TYPE(*ACCPTH) +                       
                          OUTPUT(*OUTFILE) OUTFILE(QTEMP/RELATE2) +             
                          OUTMBR(*FIRST *REPLACE)                               

This will create the file QAFDACCP. "Hey, where did I figure THAT information out?" Well, I'll SHOW you! On the iSeries, since the V4 days, ALL the hard-to-find reference material -- such as the names of the file names created by CL commands or the CPF error messages that a command may produce -- are built into the help text. So hitting help in SEU, then asking for extended help produces the following text produced for the DSPFFD command:

Clicking fast forward to the File information:

Here's the line I NEEDED blown up:

Here's the Access Path File name -- QAFDACCP. Why do I need that? TO COMPILE IN BATCH!

This is needed for the following line of the RPG program:

     FQAFDACCP  IP   E             DISK                                                               

To ACCESS the file created, I overrode to see the file I created in the CL portion:

             OVRDBF     FILE(QAFDACCP) TOFILE(QTEMP/RELATE2) +                  
             DSPFD      FILE(*LIBL/&FILE) TYPE(*ACCPTH) +                       
                          OUTPUT(*OUTFILE) OUTFILE(QTEMP/RELATE2) +             
                          OUTMBR(*FIRST *REPLACE)                               


Here's a sample of the output from the example program:

      * Type of file *PHY                                                                                                                                                                                                           
      * Maintenance is Immed                                                                                                                                                                                                        
      * Non-Unique Key                                                                                                                                                                                                              
      * Key for file HTPEOPLEPF                                                                                                                                                                                                     
     C     kyHTPEOPLEPF  Klist                                                                                                                                                                                                      
     C                   KFLD                     HTHPHONE                      

So I have everything I need in my program for the KLIST portion -- or wherever I place the KLIST to see what this file is all about -- including if the field is ascending or descending.

Other examples I've written over the years can be found here. These read the DSPFFD output into the file named QADSPFFD:

Again these are examples of what I needed over the years -- I've added tools to this to complete my cache.

Here's something else you might want to play with. Give it a try, and if you have any questions or comments or are confused, e-mail the editor and I'll get right back to you.

Command for CR8CSPEC;
             CMD        PROMPT('Create C Specs')                                                      
             PARM       KWD(FILE_NAME) TYPE(*CHAR) LEN(10) MIN(1) +                                   
                          CHOICE('Valid File Name') PROMPT('Enter a +                                 
                          File for Info')                                                             
             PARM       KWD(DEF_FIELDS) TYPE(*CHAR) LEN(1) +                                          
                          RSTD(*YES) DFT('0') VALUES('1' '0') +                                       
                          CHOICE('1=Yes, 0=No') PROMPT('Provide +                                     

About the author: Andrew Borts is webmaster at United Auto Insurance Group in North Miami, Fla. He is often a frequent speaker at COMMON and is past president of The Southern National Users Group, an iSeries-AS/400 user group based in Deerfield Beach, Fla.

Dig Deeper on RPG iSeries programming

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.