Deleting old files using scripting

Document ID : KB000090063
Last Modified Date : 14/04/2018
Show Technical Document Details
Issue:
Deleting old files using scripting
Resolution:
Detailed Description and Symptoms

Sometimes there is a need to remove files from a directory that are over a certain amount of days old.  Below is an example of Automic scripting that can be put into the Process tab of a job that will delete the files in a certain directory based on their date on the same machine as the agent used in the job.  In the example below, all files older than 31 days (set with the &AGE# variable) that are in the C:\deleteme folder (set with the &FOLDER# variable) will be deleted.  These two variables will need to be changed based on the needs of the job.

PLEASE NOTE: The following is only an example of how to do this and should be tested in a test environment before being placed into a production environment.  Automic Support is not responsible for the results of use or for maintenance or implementation of this scripting.  If a dedicated resource is required to implement scripting or come up with specific scripts, please contact Automic Support and they can contact your Account Manager or CRM in order to have an engagement scheduled with the Automic consulting team which may require a fee.


Solution

Place the following in the Process tab of a job:

!Set variables:
!AGE is the oldest file you would like to keep (i.e. nothing older than &AGE# days)
:SET &AGE#     = '31'
!Set the folder that the files are in
:SET &FOLDER#  = 'C:\deleteme'
!Find the host this is working on
:SET &HOST#    =GET_ATT(HOST)
!Today's date
:SET &TODAY#   = SYS_DATE('YYYYMMDD')
!The login
:SET &LOGIN#   = GET_ATT(LOGIN)
!------------------------------------------------------------------
!Find the oldest date for files you want to keep (Today minus &AGE# days)
:SET &OLD#     = SUB_DAYS('YYYYMMDD:&TODAY#',&AGE#)
!Specify the directory in which to find files/folders to be deleted.
:PUT_ATT WIN_WORK_DIR="&FOLDER#"
:SET &FOLD# = GET_ATT(WIN_WORK_DIR)
!Prep process through a dir command of &FOLDER#
:SET &HND#     = PREP_PROCESS("&HOST#","WINCMD","*/*","CMD=DIR /A-D &FOLDER#","UC_LOGIN=&LOGIN#")
!Start the processing
:PROCESS &HND#
!Get the line for each line that was output in the dir command
:  SET &LINE#     = GET_PROCESS_LINE(&HND#)
!Find the date of when the file was created (not modified) (first 10 characters)
:  SET &FILEDATE# = SUBSTR(&LINE#,1,10)
!Convert the date
:  SET &FILEDATE# = CONV_DATE('MM/DD/YYYY:&FILEDATE#','YYYYMMDD')
!If the date of the file is
:  IF  &FILEDATE#  < &OLD#
!Find the actual file name (start 40 characters out)
:    SET &FILENAME# = SUBSTR(&LINE#,40)
:    P "deleting &FILENAME#"
!Process the deletion of each file with a del command
:    SET &DEL# = PREP_PROCESS("&HOST#","WINCMD",,"CMD=DEL ""&FOLDER#\&FILENAME#""","UC_LOGIN=&LOGIN#")
!Close the deletion process
:    CLOSE_PROCESS &DEL#
:  ENDIF
:ENDPROCESS
 
@set retcode=%errorlevel%
@if NOT %ERRORLEVEL% == 0 goto :retcode