How and Why to Copy Spooled Files

IT Infrastructure - Other
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Copying spooled files can be easily accomplished using standard CL. It has its advantages and limitations. Learn how, when and why to use this technique.

Spooled files do not reside on the AS/400 as objects; therefore, they can't be manipulated or even saved to offline storage like other objects. However, spooled files can be copied to and from physical files, providing an indirect path to manipulate them.

Copying a spooled file to a physical file and back to a spooled file again can be easily accomplished using a few standard CL commands. This technique is not revolutionary. In fact, it has been around for a long time. Many utilities written over the years have been built around this concept (e.g., the SAVSPLF and RSTSPLF commands published in the October 1991 issue of MC).

In this article, I want to do more than just explain what steps you need to perform to accomplish this task. I want to give you some reasons for using this technique, as well as some warnings about when not to use it.

Possible Uses

A typical reason for wanting to copy a spooled file is for archival purposes. When you save an output queue using one of the save commands (e.g., SAVLIB, SAVOBJ), any spooled files within that output queue are not saved. By copying the spooled file to a physical file, you can then save the file to tape or diskette.

Subsequently, if you find that you need to print the spooled file but it is no longer on the system, you can restore the file, copy it back to a spooled file again and print it.

For your typical, text-based spooled files such as standard RPG output or system commands which provide OUTPUT(*PRINT), this technique works well. But, if a spooled file contains elements like graphics, barcodes, special fonts or Intelligent Printer Data Stream (IPDS) data, they would be lost when a spooled file is copied. For a better technique to handle these types of spooled files, see "Beyond CPYSPLF-Saving Everything in a Spooled File" in this issue.

Another use for copying a spooled file is the ability to change its attributes. Most attributes of a spooled file can be changed using the Change Spooled File Attributes (CHGSPLFA) command. However, you might want to modify a few attributes (most commonly, characters per inch) that the CHGSPLFA command cannot control.

For example, suppose you ran a 12-hour job which produced a 132-column report at 10 characters per inch. This report would print perfectly on 11" x 17" computer paper, but what if you need to print it on 8-1/2" x 11" paper? You could rerun the 12-hour job. Or, you could copy the spooled file to a physical file and then copy it back to a spooled file at 15 characters per inch in a matter of minutes.

Yet another reason for wanting to copy a spooled file is to modify the spooled file's contents. When a spooled file has been copied to a physical file, you can change its contents using DFU or any file utility. Then the contents of the physical file can be copied back to a spooled file again with any changes that have been made still in effect.

Obviously, there are some security issues to be dealt with here. For example, suppose a spooled file contains payroll checks. An employee with access to the spooled file who knows this technique could conceivably change the dollar amount on his paycheck. Therefore, you need to be sure you have adequate security measures in place. For some ideas on how to secure spooled files, see "Security Patrol" in this issue.

However, there are legitimate reasons for wanting to modify a spooled file. A spooled file that needs minor corrections or that you want to use for documentation is a good example. (There is a utility in QUSRTOOL called Copy Spooled File to Text Member [CPYSPLTXT] which allows you to perform these functions.) A limitation to copying a spooled file is that it doesn't save the spooled file attributes. This shouldn't be a problem for standard reports; but not having the attributes for special reports may prevent you from restoring them with the same attributes. There are ways to get around this, as I will explain later.

Another limitation to copying a spooled file is that it allows only one spooled file to be copied at a time. For copying and restoring an entire OUTQ at one time, you might consider using the Copy from Output Queue (CPYFRMOUTQ) and Copy to Output Queue (CPYTOOUTQ) commands from the QUSRTOOL library. Both of these commands are discussed in this month's "From the Toolbox."

Here's How to Do It

Before you copy a spooled file, you need to determine how many columns it contains. Use the Work with Spooled File Attributes (WRKSPLFA) command to find out the page width. You then need to create a physical file to contain the spooled file data. Create the physical-file record length one byte larger than the page width. The reason for this is that the first character of the physical file gets loaded with a First Character Forms Control (FCFC) character. These FCFC characters control the line spacing and page breaks of the spooled file. The rest of the positions of the physical file get loaded with the actual spooled file data.

It is a good idea to specify the name of the printer file which was used to create the spooled file in the TEXT parameter of the Create Physical File (CRTPF) command. That way, you will know what printer file to use when you want to obtain the attributes to recreate the spooled file. You can copy the spooled file to the physical file using the Copy Spooled File (CPYSPLF) command. Be sure to use *FCFC for the Control Character (CTLCHAR) parameter to specify that you want the FCFC character to be placed in the first position of the physical file. 1 contains an example.

It is a good idea to specify the name of the printer file which was used to create the spooled file in the TEXT parameter of the Create Physical File (CRTPF) command. That way, you will know what printer file to use when you want to obtain the attributes to recreate the spooled file. You can copy the spooled file to the physical file using the Copy Spooled File (CPYSPLF) command. Be sure to use *FCFC for the Control Character (CTLCHAR) parameter to specify that you want the FCFC character to be placed in the first position of the physical file. Figure 1 contains an example.

Recreating a Spooled File

Once you have copied a spooled file to a physical file, you can save it or modify the data in the physical file. At some point, you may want to copy the physical file back to a spooled file.

To do this, you basically use the Copy File (CPYF) command to copy the physical file to a printer file. However, there are two variations in the way you do this.

The method used depends on what type of printer file (program-described or externally described) you used to create the spooled file and if any overrides were in effect at the time the spool file was created.

1. If the printer file used to create the original spooled file is program- described and no overrides were used when the spooled file was created, use the original printer file name in the TOFILE parameter of the CPYF command. If any attributes were overridden at the time the spooled file was created, you must execute an Override Printer file (OVRPRTF) command to change the attributes of the printer file to match the values used at spooled-file creation time.

2. For spooled files created with externally described printer files, the method is a little different. The TOFILE parameter of CPYF cannot contain an externally described printer file. Therefore, instead of using the original printer file name in the TOFILE parameter, use a program-described printer file that closely matches the attributes of the report (for standard reports, QPRINT or QSYSPRT should work). Match the attributes from the program-described file to the attributes from the external printer file, if there are any mismatches, by using the Override Printer File (OVRPRTF) command prior to performing the copy. Also, override any parameters that may have been overridden at the time the spooled file was created.

The CPYF command will translate the FCFC character to the correct line spacing and page breaks for the spooled file if the CTLCHAR attribute of the printer file is *FCFC. This can be accomplished by specifying CTLCHAR(*FCFC) on either the Create Printer File (CRTPRTF) command, the Change Printer File (CHGPRTF) command or the Override Printer File (OVRPRTF) command.

Use the Display File Description (DSPFD) command on the printer file to determine the current setting of the CTLCHAR parameter. Since *NONE is the default parameter on the CRTPRTF and CHGPRTF commands, you will most likely need to execute the OVRPRTF command.

After the CPYF, you will have a spooled file in the OUTQ associated with your job. The lines of code in 2 illustrate how to recreate a spooled file.

After the CPYF, you will have a spooled file in the OUTQ associated with your job. The lines of code in Figure 2 illustrate how to recreate a spooled file.

Some Examples

Let's take a look at an example of how you can use this technique to archive your spooled files to tape or diskette. To save a spooled file, first copy it to a physical file and then save it. To get the spooled file back, restore it and then copy it back to a physical file. The commands would look something like those in 3.

Let's take a look at an example of how you can use this technique to archive your spooled files to tape or diskette. To save a spooled file, first copy it to a physical file and then save it. To get the spooled file back, restore it and then copy it back to a physical file. The commands would look something like those in Figure 3.

In order to change the attributes of a spooled file, you need to copy the spooled file to a physical file. Copy the physical file to a printer file and specify the new attributes using OVRPRTF. The commands for this example appear in 4.

In order to change the attributes of a spooled file, you need to copy the spooled file to a physical file. Copy the physical file to a printer file and specify the new attributes using OVRPRTF. The commands for this example appear in Figure 4.

To modify a spooled file, copy it to a physical file just as before. Then modify the data in the physical file; and when you copy the physical file to the printer file, the newly recreated spooled file will contain the changes you made. 5 contains commands you would use in this case.

To modify a spooled file, copy it to a physical file just as before. Then modify the data in the physical file; and when you copy the physical file to the printer file, the newly recreated spooled file will contain the changes you made. Figure 5 contains commands you would use in this case.

To Sum It Up

As you can see by the techniques outlined in this article, copying spooled file data to and from physical files only takes a few CL commands. It allows you to perform various tasks on spooled files such as archiving, changing attributes and modifying contents. However, it does have limitations in that it won't work for all spooled files.

Now that you know under what conditions this technique can be used, you can begin to take advantage of its capabilities while avoiding its limitations.

Robin Klima is a senior technical editor at Midrange Computing.


How and Why to Copy Spooled Files

Figure 1 Copy from Spooled File

 /* COPY A SPOOLED FILE TO A PHYSICAL FILE */ CRTPF FILE(XXX/SPOOL) RCDLEN(133) TEXT('QPRINT') CPYSPLF FILE(QPRINT) TOFILE(XXX/SPOOL) + JOB(number/user/job) CTLCHAR(*FCFC) 
How and Why to Copy Spooled Files

Figure 2 Copy to Spooled File

 /* COPY PHYSICAL FILE BACK TO A SPOOLED FILE */ OVRPRTF FILE(QPRINT) CTLCHAR(*FCFC) CPYF FROMFILE(XXX/SPOOL) TOFILE(QPRINT) 
How and Why to Copy Spooled Files

Figure 3 Spooled File Save/Restore

 /* COPY A SPOOLED FILE TO A PHYSICAL FILE */ CRTPF FILE(XXX/SPOOL) RCDLEN(133) TEXT('QPRINT') CPYSPLF FILE(QPRINT) TOFILE(XXX/SPOOL) + JOB(number/user/job) CTLCHAR(*FCFC) /* SAVE THE PHYSICAL FILE TO TAPE */ SAVOBJ OBJ(SPOOL) LIB(XXX) DEV(TAP01) OBJTYPE(*FILE) . . . /* RESTORE THE PHYSICAL FILE FROM TAPE */ RSTOBJ OBJ(SPOOL) SAVLIB(XXX) DEV(TAP01) OBJTYPE(*FILE) /* COPY PHYSICAL FILE BACK TO A SPOOLED FILE */ OVRPRTF FILE(QPRINT) CTLCHAR(*FCFC) CPYF FROMFILE(XXX/SPOOL) TOFILE(QPRINT) 
How and Why to Copy Spooled Files

Figure 4 Change a Spooled File's Attribute (CPI)

 /* COPY A SPOOLED FILE TO A PHYSICAL FILE */ CRTPF FILE(XXX/SPOOL) RCDLEN(133) TEXT('QPRINT') CPYSPLF FILE(QPRINT) TOFILE(XXX/SPOOL) + JOB(number/user/job) CTLCHAR(*FCFC) /* COPY PHYSICAL FILE BACK TO A SPOOLED FILE WITH NEW ATTRIBUTES */ OVRPRTF FILE(QPRINT) CPI(15) CTLCHAR(*FCFC) CPYF FROMFILE(XXX/SPOOL) TOFILE(QPRINT) 
How and Why to Copy Spooled Files

Figure 5 Modify a Spooled File's Contents

 /* COPY A SPOOLED FILE TO A PHYSICAL FILE */ CRTPF FILE(XXX/SPOOL) RCDLEN(133) TEXT('QPRINT') CPYSPLF FILE(QPRINT) TOFILE(XXX/SPOOL) + JOB(number/user/job) CTLCHAR(*FCFC) /* MODIFY THE SPOOLED FILE DATA IN THE PHYSICAL FILE */ UPDDTA FILE(XXX/SPOOL) /* COPY PHYSICAL FILE BACK TO A SPOOLED FILE */ OVRPRTF FILE(QPRINT) CTLCHAR(*FCFC) CPYF FROMFILE(XXX/SPOOL) TOFILE(QPRINT) 
BLOG COMMENTS POWERED BY DISQUS