AS/400 Links Partner with Rochester Institute Contact Us
Training Catalog Free Downloads Privacy / Usage

FREE AS/400, iSeries, and System i / iSeries Downloads
HOME >> FREE AS/400, iSeries, and System i / iSeries Downloads >> Use Program Cloning To Lessen Your Workload

Use Program Cloning To Lessen Your Workload
Author: G. Wayne Hawks

What if you had a program that could write programs? Imagine what you could do. Suppose you need to change one thing or count items in a whole bunch of files. Or maybe you want to delete specific records in a number of files. Rather than writing a lot of separate programs to do these tasks, wouldn't it be great if you could just write one program and have another program make copies of it that were modified to work on each of those files? As you might have guessed, all this is possible and it's the subject of this article.

But, first the disclaimer: This is a great tool for your toolbox, but you need to be careful. Think of it as a sharp tool that could cut you if you're not careful. Make sure your system is backed up.

The first thing to remember is that source code itself is data. It's organized in files with multiple members. All source files (regardless of their length) each have three fields: SRCSEQ, SRCDTA, and SRCDAT. SRCSEQ is the field that contains the line number, SRCDAT contains the date that line was last changed (in YYMMDD order), and SRCDTA has the actual code lines.

With that in mind, suppose your iSeries has multiple company Divisions data in most of its files. Each file has a company Division number field to keep Division 1 separate from Division 2. You need to count how many records in each of your files contain Division 1, Division 2. Maybe Division 2 is doing so well it is getting its own iSeries. Now they guys over in Division 2 want to copy their data off the main system. But, how much data is there and which files contain it?

As I'm sure you know, each physical file probably has a different name and each physical file probably has a different format name. Each file may have a different field name that contains the Division number as well.

To "drive" the cloning process, you need a file that contains the library, file, format, and field names to put in each copy of the program.

Step 1: Use OS/400's DSPFFD (Display File Field Description) command to an output file for all files in all the libraries that contain the files you're interested in. Be sure to use *add and not *replace each time you do the DSPFFD command. The resulting output file has almost all the fields you need. Those fields it contains are listed below with the field lengths in parentheses.

WHLIB is the Library Name (10)

WHFILE is the File Name (10)

WHFTYP is the Type of File. (1)

WHNAME is the Record Format Name (10)

WHTEXT is the File's Description (50)

WHFLDI is the Field's Name (10)

WHFTXT is the Field's Description (50)

WHFLDD is the number of digits (5,0)

WHFLDP is the number of decimals (2,0)

Step 2: Create another file with all the fields listed in your output file. Be sure that they have the same names and lengths. Add a field to indicate you want to use that record. Maybe call the field WHWANTS (1). Then, copy the first file to this second file using *map and *drop and where WHFTYP *EQ 'P'. This way, you will only copy records about Physical files. There's no sense in building programs for logical files since they don't contain any data. You just need programs for the physical files.

Step 3: Now that you have your "universe" of all fields in all physical files in the libraries you are interested in, you need to reduce that group to just the ones you want. To do that, you need to be creative and find a way to select only the fields you want. For example, suppose you know that all your company number fields are a certain length, such as 3 long with 0 decimal places. And say you know that the company number fields always have field names that look like xxCO# (where xx stands for anything). In this case, you might create a sub-file program that loads into the sub-file only those records whose fields are 3 long with 0 decimal places and where the field name's third through fifth positions are CO#. Be sure to include the new WHWANTS field as an updatable field. Then, put an X or a Y, or something else to indicate you do want that field. If you don't want to type that much, you can set it up so you type an N you don't want a field. The point here is to give yourself a way to select the fields you want. The fields displayed in the subfile are your candidates. (You get to decide which ones that merit programs.)

Step 4: Either copy the file out to another file where WHWANTS = 'Y', or X' or isn't 'N' (depending on which method you chose in Step 3 to indicate you want the field). Or, create a logical file to only select those fields you want. Use this file to drive your CL program.

Step 5: Create a model RPG program with File, Format, and Field names that are easily identifiable from the rest of the code. I used the words RPLFIL, RPLFRM, and RPLFLD, but you can use anything you want. Have the model program output one record to a new file containing counts of whatever you want, such as each company number found. You only need to write to this file once at the end of the program. (See MODEL in the source file sample.)

Step 6: Create your RPG program to modify the copies of the original model program. You'll want to use the SCAN op code to easily find what to replace. (See MODMODEL in the source file sample.)

Step 7: Create an RPG program, which creates new source member names for you. (See MODMODELNM in the source file sample.)

Step 8: Create a new source file to put your cloned source into. This step makes it much easier to clean out when you want to use it again. Also create a new library to put your cloned objects into. For the same reason, it's a lot easier to just clear a library.

Step 9: Create the CL program that calls the program to get a new name, does the copying of the model program to the new source member name (in the new source file you create in step 8 above), overrides to the new source, calls the modifying program, compiles the new program and then calls or submits it. (See MODMODELC in the source file sample.)

Step 10: Important: Back up your entire system now. Get your favorite beverage, submit the program and watch the cloning process closely.

Step 11: If you're really brave and you've checked the results of your output file and everything looks great, reset the file that holds the program number counter, delete all the cloned source, clear the library with all the cloned objects, modify the model program to delete instead of count the records, and you could use this process to remove all that other company's records. It's extremely important to do a backup before you run the program.

Cloning programs takes some homework, but it's an efficient way to get a lot of work done in a hurry. If you've done your homework well, it's amazing to watch programs writing programs, all doing exactly what you want. If didn't do your homework, well, it's like a cloning experiment gone very wrong. Don't say I didn't warn you when that monster sneaks up behind you.

G. Wayne Hawks is a programmer/analyst with 16 years of experience in AS/400 programming.