ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Looking for CL that will delete files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looking for CL that will delete files

    Newbie...sorry

    Can anyone offer some help on automatically deleting a range of files after a monthly backup?

    Thanks

  • #2
    Re: Looking for CL that will delete files

    deleting files every month...... I dont think i like where this is going..
    please explain more.

    jamie
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Re: Looking for CL that will delete files

      Originally posted by jamief View Post
      deleting files every month...... I dont think i like where this is going..
      please explain more.

      jamie


      Thanks for the respone Jamie

      Here are the cliffnotes...

      We have a library that needs to be cleaned up monthly due to a large number of daily files that are being created. The nightly backups are taking longer and longer and our window is getting smaller and smaller.

      Right now I am going in and deleting each file manually...which is really time consuming and a complete waste of time.....much like this thread. But I am new, so please bear with me

      Comment


      • #4
        Re: Looking for CL that will delete files

        Ideally, the program that creates the temporary file should be deleting it after its purpose is served.

        Does the library have only temporary files which you don't need to back up? Then, can the entire library be excluded from being backed up?
        If there is a pattern of naming the temporary files, like TMP*****, and no other file (that you need to back up) has the same name, you can do:
        Code:
        DLTF lib/TMP*
        â??No bird soars too high if he soars with his own wingsâ?? â?? William Blake

        Comment


        • #5
          Re: Looking for CL that will delete files

          Originally posted by kausix777 View Post
          Ideally, the program that creates the temporary file should be deleting it after its purpose is served.

          Does the library have only temporary files which you don't need to back up? Then, can the entire library be excluded from being backed up?
          If there is a pattern of naming the temporary files, like TMP*****, and no other file (that you need to back up) has the same name, you can do:
          Code:
          DLTF lib/TMP*
          Thanks for the reply...

          No, these are files that need to be backed up.

          Instead of creating a master file that records can be written to, the person that originally set this up is creating a file for each record. Why I don't know.

          Each file has an incremental log number ( +1) that is generated, but this library is growing way too fast and taking up too much space.

          Comment


          • #6
            Re: Looking for CL that will delete files

            sounds like you'd be better served to rewrite the program to use a single file. let me guess the files are also being created with the default size vs creating with maxrcds(1)
            I'm not anti-social, I just don't like people -Tommy Holden

            Comment


            • #7
              Re: Looking for CL that will delete files

              How are you saving the files? (Do you use BRMS?)
              Greg Craill: "Life's hard - Get a helmet !!"

              Comment


              • #8
                Re: Looking for CL that will delete files

                I have various routines that may help you ... this one uses a CL to control and do the purge, and an RPG to check the dates for each file. It checks for dependent logicals and in the *DLT option it will delete them before the PF.

                Hope it is something you can use ...

                -------------------------------

                This little fella does the date calculation part for you and returns *PURG if the file meets the purge dates, else it returns *KEEP. Someone can prob do a smarter one, but it works for my requirements, in fact someone clever could prob do all this in 1 RPG, but again works for me

                Code:
                H* +-------------------------------------------------------------------+
                H* | Author: Greg Craill     Created:  22 Aug 2011  Version:  Initial  |
                H* +-------------------------------------------------------------------+
                H* | Purpose:  This program takes purge offset parameters for months   |
                H* | (PrgMths) and days (PrgDays) and compares them against a sample   |
                H* | date (USEdate), it then returns the action to take (DoAction).    |
                H* | If Today - months(PrgMths) - days(PrgDays) >  USEdate then *PURG  |
                H* | If Today - months(PrgMths) - days(PrgDays) <= USEdate then *KEEP  |
                H* | The calling program will have to take the action based on the     |
                H* | DoPurge parametyer returned to it.                                |
                H* +-------------------------------------------------------------------+
                F*                                                                      
                D* ---------  Parameters In/Out  --------------                         
                D NumMths         S              3A                                     
                D NumDays         S              3A                                     
                D USEdate         S              6A                                     
                D DoPurge         S              5A                                     
                D* ---------  Other Work Variables  -----------                         
                D PRGdate         S              8  0                                   
                D CHKdate         S              8  0                                   
                D PrgMths         S              3  0                                   
                D PrgDays         S              3  0                                   
                D* Commented out the message bit - for debugging only                   
                D*MsgTxt          S             50A                                       
                                                                                          
                C     *Entry        PLIST                                                 
                C* ---------  Input Parms  --------------------                           
                C*  NumMths = The Number of Months data to keep                           
                C*  NumDays = The Number of Days data to keep                             
                C*  USEdate = The Date of Object to compare against                       
                C*  NOTE: Months and Days are added together for the total purge offset   
                C                   PARM                    NumMths                       
                C                   PARM                    NumDays                       
                C                   PARM                    USEdate                       
                C* ---------  Output Parms  -------------------                           
                C*  DoPurge = The Action to take is returned to the calling program.      
                C*            It will be either "*PURG" or "*KEEP"                        
                C                   PARM                    DoPurge                       
                                                                                          
                 /FREE                                                                    
                                                                                          
                   // Parms are passed in as *CHAR, convert to *DEC for the date functions
                   PrgMths = %Dec(NumMths:3:0) * -1;                                      
                   PrgDays = %Dec(NumDays:3:0) * -1;                                      
                   PRGDate = %Dec(%Date() + %Days(PrgDays) + %Months(PrgMths));           
                   CHKDate = %Dec(%Date(USEdate: *MDY0));                                 
                                                                              
                   If PRGdate > CHKdate;                                      
                     // MsgTxt = 'PrgDate(' + %Char(PRGdate) + ') ChkDate(' + 
                     //          %Char(CHKdate) + ') ==> Purge';              
                     DoPurge = '*PURG';                                       
                   Else;                                                      
                     // MsgTxt = 'PrgDate(' + %Char(PRGdate) + ') ChkDate(' + 
                     //          %Char(CHKdate) + ') ==> KEEP';               
                     DoPurge = '*KEEP';                                       
                   EndIf;                                                     
                   // DSPLY MsgTxt;                                           
                                                                              
                   *INLR = *ON;                                               
                                                                              
                 /END-FREE
                This CLLE chappie takes a library and purges all files in it that meet the date parms. our production libraries have ICMS in them, so we ban the program from touching them else all hell could break loose! Be VERY careful on generic purge programs that can wipe out production stuff!

                (I have a BRMS version of this that first saves and then purges to be safer)

                Code:
                /* +------------------------------------------------------------+ */ 
                /* | This program clears or deletes files in a target library   | */ 
                /* | that meet the criteria selected for last used date and     | */ 
                /* | creation date.                                             | */ 
                /* +------------------------------------------------------------+ */ 
                /* | Greg Craill   22 Aug 2011 - Initial Version                | */ 
                /* +------------------------------------------------------------+ */ 
                /* | Greg Craill   19 Sep 2011 - Changed to HOLD(*YES/*NO)      | */ 
                /* +------------------------------------------------------------+ */ 
                /* | To compile this program first do ...                       | */ 
                /* | DSPDBR     FILE(I321OPSS/OPSPURGES) OUTPUT(*OUTFILE)       | */ 
                /* |            OUTFILE(QTEMP/LFFILES) OUTMBR(*FIRST *ADD)      | */ 
                /* | DSPOBJD    OBJ(TEMPFILES/*ALL) OBJTYPE(*FILE)              | */ 
                /* |              OUTPUT(*OUTFILE) OUTFILE(QTEMP/PFFILES)       | */ 
                /* | DSPOBJD    OBJ(TEMPFILES/*ALL) OBJTYPE(*FILE)              | */ 
                /* |              OUTPUT(*OUTFILE) OUTFILE(QTEMP/PFLIST)        | */ 
                /* +------------------------------------------------------------+ */ 
                /* | Related Objects;                                           | */ 
                /* | PRGFIL      *CMD   -  Calls PRGFILC1                       | */ 
                /* | PRGFILC1    *CLLE  -  Purges Objects from TEMPFILES Lib    | */ 
                /* | PRGFILC2    *CLLE  -  Save and Purge SAVARKLIB object list | */ 
                /* | PRGDATR1    *RPGLE -  Checks file date against purge date  | */ 
                /* |                       and returns *PURG or *KEEP           | */ 
                /* +------------------------------------------------------------+ */         
                /* | Variables:                                                 | */         
                /* | I  &PrgLib    Library to Purge files from                  | */         
                /* | I  &PrgTyp    *DLT or *CLR the selected files ?            | */         
                /* | IO &PrgMths   Number of Months to purge to  ( added  )     | */         
                /* | IO &PrgDays   Number of Days to purge to    (together)     | */         
                /* | I  &JobHld    *YES or *NO to hold on job queue             | */         
                /* |  O &ChkDate   Date to check for purge or not               | */         
                /* |  O &Action    *PURG or *KEEP based on &ChkDate             | */         
                /* +------------------------------------------------------------+ */         
                                                                                             
                             PGM        PARM(&PRGLIB &PRGTYP &PRGMTHS &PRGDAYS &JOBHLD)      
                                                                                             
                             /*         Purge Library - Library name to purge from         */
                             DCL        VAR(&PRGLIB)    TYPE(*CHAR) LEN(10)                  
                             /*         Purge Type    - *DLT or *CLR the selected files    */
                             DCL        VAR(&PRGTYP)    TYPE(*CHAR) LEN(4)                   
                             /*         Purge Months  - How many months ago to purge to ?  */
                             DCL        VAR(&PrgMths)   TYPE(*CHAR) LEN(3)                   
                             /*         Purge Days    - How many months ago ?              */
                             DCL        VAR(&PrgDays)   TYPE(*CHAR) LEN(3)                   
                             /*         Purge Days    - How many months ago ?              */
                             DCL        VAR(&JobHld)    TYPE(*CHAR) LEN(4)                   
                                                                                          
                             DCL        VAR(&ChkDate)   TYPE(*CHAR) LEN(6)                
                             DCL        VAR(&Action)    TYPE(*CHAR) LEN(5)                
                             DCL        VAR(&JobTyp)    TYPE(*CHAR) LEN(1)                
                             DCL        VAR(&DLTMSG)    TYPE(*CHAR) LEN(200)              
                                                                                          
                             DCLF       FILE(QTEMP/PFLIST)  OPNID(F1)                     
                             DCLF       FILE(QTEMP/LFFILES) OPNID(LF)                     
                             DCLF       FILE(QTEMP/PFFILES) OPNID(PF)                     
                                                                                          
                /* +------------------------------------------------------------------+ */
                /* |   Cancel if ICMS library name is used.                           | */
                /* +------------------------------------------------------------------+ */
                             IF         COND(%SST(&PRGLIB 2 4) = 'ICMS') THEN(DO)         
                               SNDPGMMSG  MSG('Not allowed to use this program to +       
                                          purge from ICMS libraries. Request Cancelled!') 
                               GOTO       CMDLBL(ENDPGM)                                  
                             ENDDO                                                        
                                                                                          
                /* +------------------------------------------------------------------+ */
                /* |   Submit to Batch     &JobTyp   0 = Batch   1 = Interactive      | */
                /* +------------------------------------------------------------------+ */
                             RTVJOBA    TYPE(&JobTyp)                                     
                             IF         COND(&JOBTYP = '1') THEN(DO)                      
                               SBMJOB     CMD(CALL PGM(PRGFILC1) PARM(&PRGLIB &PRGTYP +   
                                          &PRGMTHS &PRGDAYS &JobHld)) JOB(PRGUSRLIB) +    
                                          JOBQ(MULTI) HOLD(&JobHld)                       
                               GOTO       CMDLBL(ENDPGM)                                  
                             ENDDO                                                        
                                                                                          
                /* +------------------------------------------------------------------+ */
                /* |   Log status messages                                            | */
                /* +------------------------------------------------------------------+ */
                             SNDPGMMSG  MSG('Purge Library is:' *BCAT &PrgLib)            
                             SNDPGMMSG  MSG('Purge Type is:' *BCAT &PrgTyp)               
                             SNDPGMMSG  MSG('Purge Date is:' *BCAT &PRGMTHS *BCAT +       
                                          'months and' *BCAT &PRGDAYS *BCAT 'days.')      
                                                                                          
                /* +------------------------------------------------------------------+ */
                /* |   Clear Temp Work files                                          | */
                /* +------------------------------------------------------------------+ */
                             CLRPFM     FILE(QTEMP/PFFILES)                               
                             MONMSG     MSGID(CPF0000)                                    
                             CLRPFM     FILE(QTEMP/LFFILES)                               
                             MONMSG     MSGID(CPF0000)                                    
                                                                                                  
                /* +------------------------------------------------------------------+ */        
                /* |   List all files in target library to file PFLIST.               | */        
                /* +------------------------------------------------------------------+ */        
                             DSPOBJD    OBJ(&PRGLIB/*ALL) OBJTYPE(*FILE) +                        
                                          OUTPUT(*OUTFILE) OUTFILE(QTEMP/PFLIST)                  
                             MONMSG     MSGID(CPF0000) EXEC(GOTO CMDLBL(NOFILES))                 
                                                                                                  
                /* +------------------------------------------------------------------+ */        
                /* |   Read files and find ones with valid purge dates.               | */        
                /* +------------------------------------------------------------------+ */        
                             SNDPGMMSG  MSG('Checking File Dates')                                
                 READ1:      RCVF       OPNID(F1)                                                 
                             MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(EOF1))                    
                             /*  &F1_ODCDAT is the Creation Date.                */               
                             /*  &F1_ODRDAT is the Restore Date.                 */               
                             /*  &F1_ODLDAT is the Last Changed Date.            */               
                             /*  &F1_ODUDAT is the Last Used Date.               */               
                                                                                                  
                             /*  Only process for PF attributes, ignore LF, PRTF, SAVF etc   */   
                             /*  ---------------------------------------------------------   */   
                             IF         COND(&F1_ODOBAT = 'PF') THEN(DO)                          
                                                                                                  
                               /*  If non blank then priority in file dates is Last_Used,    */   
                               /*  Last_Changed, Last_Restored, File_Created.                */            
                               CHGVAR     VAR(&CHKDATE) VALUE(&F1_ODUDAT)                                  
                               IF         COND(&CHKDATE = ' ') THEN(CHGVAR VAR(&CHKDATE) VALUE(&F1_ODLDAT))
                               IF         COND(&CHKDATE = ' ') THEN(CHGVAR VAR(&CHKDATE) VALUE(&F1_ODRDAT))
                               IF         COND(&CHKDATE = ' ') THEN(CHGVAR VAR(&CHKDATE) VALUE(&F1_ODCDAT))
                               CALL       PGM(PRGDATR1) PARM(&PRGMTHS &PRGDAYS &CHKDATE &ACTION)           
                                                                                                           
                               /*  If program PRGDATR1 returns *PURG then add the file       */            
                               /*  details to the output files.                              */            
                               IF         COND(&ACTION = '*PURG') THEN(DO)                                 
                                 DSPOBJD    OBJ(&PRGLIB/&F1_ODOBNM) OBJTYPE(*FILE) +                       
                                            OUTPUT(*OUTFILE) OUTFILE(QTEMP/PFFILES) +                      
                                            OUTMBR(*FIRST *ADD)                                            
                                 MONMSG     MSGID(CPF0000)                                                 
                                 DSPDBR     FILE(&PRGLIB/&F1_ODOBNM) OUTPUT(*OUTFILE) +                    
                                            OUTFILE(QTEMP/LFFILES) OUTMBR(*FIRST *ADD)                     
                                 MONMSG     MSGID(CPF0000)                                                 
                                 /*   SNDPGMMSG  MSG('File(' *TCAT &F1_ODOBNM *TCAT ') Date(' +   */       
                                 /*              *TCAT &CHKDATE *TCAT ') => Purge')               */       
                               ENDDO                                                                       
                               ELSE       CMD(DO)                                                          
                                 /*   SNDPGMMSG  MSG('File(' *TCAT &F1_ODOBNM *TCAT ') Date(' +   */       
                                 /*              *TCAT &CHKDATE *TCAT ') => Keep')                */       
                               ENDDO                                                              
                                                                                                  
                             ENDDO                                                                
                             GOTO       CMDLBL(READ1)                                             
                /* +------------------------------------------------------------------+ */        
                 EOF1:                                                                            
                                                                                                  
                     /* +------------------------------------------------------------------+ */   
                     /* |   Action = *CLR (Clear)                                          | */   
                     /* |   - Create spoolfile report                                      | */   
                     /* |   - Read file PFFILES and do CLRPFM on them all.                 | */   
                     /* +------------------------------------------------------------------+ */   
                             IF         COND(&PrgTyp = '*CLR') THEN(DO)                           
                               /*  Create spoolfile listing all physical files               */   
                               RUNQRY     QRY(*NONE) QRYFILE((QTEMP/PFFILES)) OUTTYPE(*PRINTER)   
                               /*  Read PFFILES and clear the physical files                 */   
                 READ2:        RCVF       OPNID(PF)                                               
                               MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(EOF2))                  
                               IF ((&PF_ODLBNM *NE ' ') *AND (&PF_ODOBNM *NE ' ')) THEN(DO)       
                                 CLRPFM     FILE(&PF_ODLBNM/&PF_ODOBNM)                           
                                 MONMSG     MSGID(CPF3133)    /* No members in file to clear */   
                               ENDDO                                                              
                               GOTO       CMDLBL(READ2)                                           
                 EOF2:                                                                          
                             ENDDO                                                              
                     /* +------------------------------------------------------------------+ */ 
                                                                                                
                                                                                                
                     /* +------------------------------------------------------------------+ */ 
                     /* |   Action = *DLT (Delete)                                         | */ 
                     /* |   - Create spoolfile report                                      | */ 
                     /* |   - Read file LFFILES and delete any dependent files listed      | */ 
                     /* |   - Read file PFFILES and delete the physical files listed       | */ 
                     /* +------------------------------------------------------------------+ */ 
                             IF         COND(&PrgTyp = '*DLT') THEN(DO)                         
                               /*  Create spoolfile listing all physical files               */ 
                               RUNQRY     QRY(*NONE) QRYFILE((QTEMP/PFFILES)) OUTTYPE(*PRINTER) 
                               /*  Create spoolfile listing all logical files                */ 
                               RUNQRY     QRY(*NONE) QRYFILE((QTEMP/LFFILES)) OUTTYPE(*PRINTER) 
                               /*  Read LFFILES and delete logical files                     */ 
                 READ3:        RCVF       OPNID(LF)                                             
                               MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(EOF3))                
                               IF ((&LF_WHRELI *NE ' ') *AND (&LF_WHREFI *NE ' ')) THEN(DO)     
                                 DLTF       FILE(&LF_WHRELI/&LF_WHREFI)                         
                               ENDDO                                                            
                               GOTO       CMDLBL(READ3)                                         
                 EOF3:                                                                           
                               /*  Read PFFILES and delete physical files                    */  
                 READ4:        RCVF       OPNID(PF)                                              
                               MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(EOF4))                 
                               IF ((&PF_ODLBNM *NE ' ') *AND (&PF_ODOBNM *NE ' ')) THEN(DO)      
                                 DLTF       FILE(&PF_ODLBNM/&PF_ODOBNM)                          
                               ENDDO                                                             
                               GOTO       CMDLBL(READ4)                                          
                 EOF4:                                                                           
                                                                                                 
                             ENDDO                                                               
                /* +------------------------------------------------------------------+ */       
                                                                                                 
                 NOFILES:                                                                        
                 ENDPGM:                                                                         
                             ENDPGM
                And then the command to call it, note I have a *CLR or *DLT option. The clear option is used where in some cases we want to keep the empty file so other programs can use it later, *DLT is well - delete like!

                Code:
                             CMD        PROMPT('Delete or Clear Files')                              
                                                                                                     
                                                                                                     
                             /*         Purge Library - Library name to purge from         */        
                             PARM       KWD(PRGLIB) TYPE(*CHAR) LEN(10) MIN(1) +                     
                                          PROMPT('Library to Purge From')                            
                                                                                                     
                             PARM       KWD(PRGTYP) TYPE(*CHAR) LEN(4) RSTD(*YES) +                  
                                          VALUES(*CLR *DLT) MIN(1) PROMPT('Clear or Delete')         
                                                                                                     
                             PARM       KWD(PRGMTHS) TYPE(*CHAR) LEN(3) DFT(12) +                    
                                          PROMPT('Months of data to keep')                           
                                                                                                     
                             PARM       KWD(PrgDays)   TYPE(*CHAR) LEN(3) DFT(0) +                   
                                          PROMPT('Days of data to keep')                             
                                                                                                     
                             PARM       KWD(JOBHLD) TYPE(*CHAR) LEN(4) RSTD(*YES) +                  
                                          DFT(*YES) VALUES(*YES *NO) PROMPT('Hold on Job Queue?')
                Greg Craill: "Life's hard - Get a helmet !!"

                Comment


                • #9
                  Re: Looking for CL that will delete files

                  Greg - thank you very much for your help.

                  We're getting BRMS with our new system which is scheduled to go live in a few months.

                  Is there a function in BRMS that can do this?

                  Comment


                  • #10
                    Re: Looking for CL that will delete files

                    @Rojo .... Nope. I wrote a few "plugins" for BRMS to expand it's functionality. For this particular example I use BRMS to manage backup lists for purging (then i don't have to write/maintain maintenance pgms for the purge parm files - sneaky huh) and have a CL that initiates the backup of the list, then reads the list items into the purge program.
                    Greg Craill: "Life's hard - Get a helmet !!"

                    Comment

                    Working...
                    X