Tips and Techniques: Delete Old Spool Files

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

It's the end of the year, so now is a good time to clean up your DASD. Often, I find that when I create a compiler listing or sample report, I leave the corresponding spool file out there for what seems like forever. So I created the Delete My Old Spool Files (DLTOLDSPLF) command.

The DLTOLDSPLF command allows you to easily clean up older, unneeded spool files. You pass in the number of days old a spool file has to be before it is deleted and optionally a user profile name, and poof, they're gone!

To delete my own spool files that are at least 30 days old, I would run the following command:

DLTOLDSPLF  DAYS(30)

Of course, you should only delete your own user profile's spool files, so the USRPRF parameter defaults to USRPRF(*CURRENT). But because we're programmers, we can, of course, delete almost anything we want (Sarbanes-Oxley, turn your head for a second). To delete all the spool files that are at least 60 days old for the user profile named BOB, you would run the following command:

DLTOLDSPLF  DAYS(60) USRPRF(BOB)

Provided you have regular OS/400 or i5/OS authorization to these spool files, poof, they're gone!

I'm using the CRTSPLLIST() procedure from the xTools *SRVPGM to build the list of spool file entries. If you don't have that service program installed, you can call the QUSLSPL API, the prototype for which is included at the end of this article. Using the CRTSPLLIST procedure reduces the parameters that I need to specify. It also saves space in this newsletter and allows me to publish more powerful code with fewer statements. In a few weeks, I will present the Delete Old IFS Files (DLTOLDIFS) command, which deletes files from a specific directory of the IFS once they become a specified number of days old.

The source code for DLTOLDSPLF is available online, but I have reproduced it here.

The command definition source code for the DLTOLDSPLF command is listed below.

DLTOLDSPLF: CMD        PROMPT('Delete my old spool files')

             /*   Command processing program is: DLTOLDSPLF */

             PARM       KWD(DAYS) TYPE(*UINT2) DFT(14) EXPR(*YES) +
                          PROMPT('Number of days to qualify')
             PARM       KWD(USRPRF) TYPE(*NAME) LEN(10) +
                          DFT(*CURRENT) SPCVAL((*CURRENT)) +
                          EXPR(*YES) PROMPT('User profile')

To view this source member online or to download it directly, follow this link.

The RPG IV source code for the DLTOLDSPLF program is listed below:

H BNDDIR('XTOOLS/RPGLIB' : 'QC2LE')
H  OPTION(*SRCSTMT:*NODEBUGIO)
H  DFTACTGRP(*NO) ACTGRP('XTOOLS')

 *****************************************************
 ** An example program that uses the RPG xTools.    **
 ** ----------------------------------------------- **
 ** Delete Old Spool Files                          **
 *****************************************************

 **  See also...
 **   QCMDSRC(DLTOLDSPLF)


D DltOldSplf      PR
D  nDaysOld                      5U 0 OPTIONS(*NOPASS)
D  szUsrPrf                     10A   OPTIONS(*NOPASS)

D DltOldSplf      PI
D  nDaysOld                      5U 0 OPTIONS(*NOPASS)
D  szUsrPrf                     10A   OPTIONS(*NOPASS)

 /INCLUDE XTOOLS/QCPYSRC,cprotos
 /INCLUDE XTOOLS/QCPYSRC,utils
 /INCLUDE XTOOLS/QCPYSRC,lists


D MySplf          S                   LIKE(RTK_SFL0200)

 **  How long before we delete the SPLF?
D DFTExpired      C                   Const(14)

D szUser          S             10A   Inz(*USER)
D today           S               D   Inz(*SYS)
D crtDate         S               D   DatFmt(*ISO)
D nExpired        S             10I 0
D nDays           S             10I 0
D nSplf           S             10I 0
D nPos            S             10I 0
D I               S             10I 0
D dltsplf         S             64A   Varying

 **  All DLTSPLF commands that are run, are
 **  posted to the joblog as informational messages.
D cmdShell        C                   Const('DLTSPLF FILE(%s) JOB(+
D                                           %s/%s/%s) SPLNBR(%s)')

C                   eval      *INLR = *ON

 **  If the caller passed in the number of days-old to delete,
 **  use that value, otherwise use the default of 14 days.
C                   if        %Parms >= 1
C                   eval      nExpired = nDaysOld
C                   else
C                   eval      nExpired = DFTExpired
C                   endif

 **  If the caller passed in a specific user profile,
 **  use that profile, otherwise use the default *CURRENT.
C                   if        %Parms >= 2
C                   if        szUsrPrf <> *BLANKS
C                              and %subst(szUsrPrf:1:1) <> '*'
C                   eval      szUser   = szUsrPrf
C                   endif
C                   endif

 **  Create a list of spool files for the user profile.
 **  The xTools SPLF0200 format is returned. Note that this format
 **  is a consolidation of the screwy format that IBM created.
 **  On later releases of OS/400, SPLF0300 was added by IBM to do
 **  what is already being accomplished by the SPLF0200 format in xTools.
 **  Since I want this to work on V4R5 and later, I'm using the xTools.

C                   eval      nSplf = CrtSplList('*':'SPLF0200': szUser)
C                   if        nSplf <= 0
C                   callp     WrtJoblog('No spool files found for +
C                                        user %s': szUser)
C                   endif

 **  Iterate through the list of spool file entries that were generated.
 **  If the date the spool file was created is older than our "days old"
 **  setting, then issue a DLTSPLF command for that spool file.
C                   dow       GetNextEntry('*': nPos : mySplf) > 0
C                   eval      RTK_SFL0200 = mySplf
C     *CYMD0        MOVE      TK_QUSDTOPN   CrtDate
C     Today         SubDur    CrtDate       nDays:*DAYS
C                   if        nDays >= nExpired
 **  The xTools' FMTTEXT procedure allows us to easily
 **  create the DLTSPLF command string.
C                   eval      dltsplf = FmtText(cmdShell: TK_QUSSPLFNM :
C                                                 %TRIMR(TK_QUSJOBNBR) :
C                                                 %TRIMR(TK_QUSUSRNAM) :
C                                                 %TRIMR(TK_QUSJOBNAM) :
C                                                  %Char(TK_QUSSPLNBR) )
 **  Log the DLTSPLF command to the joblog before running it.
C                   callp     wrtjoblog(dltsplf)
 **  Delete the spool file.
C                   callp     system(dltsplf)
C                   endif
C                   enddo
 **  ENDPGM:
C                   return

To view this source member online or to download it directly, follow this link.

The prototype for the QUSLSPL API is as follows:

 *********************************************************
 ** E N U M E R A T E   S P O O L  F I L E  N A M E S   L I S T
 *********************************************************
D QUSLSPL         PR                  ExtPgm('QUSLSPL')
D  szUserSpace                  20A   Const
D  Format                        8A   Const
D  UserName                     10A   Const
D  OutQ                         20A   Const
D  FormType                     10A   Const
D  UserData                     10A   Const 
D  apiError                           Like(QUSEC) OPTIONS(*NOPASS)
D  JobID                        26A   Const OPTIONS(*NOPASS)
D  RtnFldKeys                   10I 0 Const OPTIONS(*NOPASS) 

D Dim(24)
D FldKeyCnt 10I 0 Const OPTIONS(*NOPASS)
D ASP 10I 0 Const OPTIONS(*NOPASS)

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$