home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / killold1.zip / KILLOLD.CMD next >
OS/2 REXX Batch file  |  1996-06-11  |  5KB  |  161 lines

  1. /* rexx file to kill old files left lying around */
  2.  
  3. /* trace results */
  4.  
  5. /*
  6.    will take either a directory name and the "age" in days or a file
  7.    with a list of directory names and "age" days. It differentiates
  8.    between the two formats by what you pass it. Either a directory
  9.    or a file name. If you pass it a listfile *and* an "age" parm it
  10.    will use the "age" as a default for each of the directories in the
  11.    listfile - which can be overridden inside the listfile.
  12.  
  13.    Requirements:
  14.     RexxUTIL.DLL - shipped with OS/2
  15.     RxDates.cmd  - date manipulation rexx file.
  16.  
  17. */
  18.  
  19.  /* setup stuff */
  20. basedir=directory()
  21. env='OS2ENVIRONMENT'
  22.  
  23.  signal on halt name errortrap
  24.  signal on syntax name errortrap
  25.  
  26.  
  27. parse source . how myfile /* how was I called? from Command line or rexx program */
  28. mypath=filespec('D',myfile) || filespec('P',myfile)
  29. parse value filespec('N',myfile) with myname '.' myextn
  30.  
  31. call postlog myname 'Checking for out of date files.'
  32.  
  33.  
  34. if right(mypath,1)='\' then mypath=left(mypath,length(mypath)-1) /* should be my directory */
  35. call directory(mypath) /* now in directory where I started from. this gives me access to RxDates.cmd */
  36.  
  37. call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  38. call SysLoadFuncs
  39.  
  40.  if \exist('RxDates.cmd') then
  41.   do
  42.    say
  43.    say 'Missing required file RxDates.cmd! Should be in the 'mypath' directory.'
  44.    say
  45.    call error
  46.   end /* do */
  47.  
  48. parse upper arg dirlist defaultage
  49.  
  50.  if length(dirlist)<1 then call error
  51.  
  52.  rc=sysfiletree(dirlist,filestem,'OD') /* is this a directory? */
  53.  
  54.  if filestem.0>0 then /* this is a directory, process directly */
  55.   do
  56.    call do_dir dirlist defaultage
  57.    exit
  58.   end /* do */
  59.  
  60.  else /* hopefully this is a listfile */
  61.  
  62.  rc=sysfiletree(dirlist,filestem,'OF') /* is this a file? */
  63.    if filestem.0=1 then
  64.     do while lines(dirlist)>0 /* listfile processing */
  65.       drop dirname age
  66.       parse value linein(dirlist) with dirname age .
  67.       if length(age)<1 then age=defaultage
  68.       if left(dirname,1) <> ';' then call killem dirname age /* allow for comments in list file */
  69.     end /* do */
  70.  
  71.    else call error
  72.  
  73.  call postlog myname 'Complete.'
  74.  
  75.  call directory(basedir)
  76.  
  77. exit
  78.  
  79. do_dir:
  80.  arg dir_spec age
  81.    rc=sysfiletree(dir_spec,dirstem2,'OD') /* is this a directory? */
  82.    do x=1 to dirstem2.0              /* handle wildcards too! */
  83.     call killem dirstem2.x age
  84.    end /* do */
  85. return
  86.  
  87.  
  88. killem:
  89.  arg targetdir daysold
  90.  
  91.  if length(daysold)<1 then call error
  92.  if \datatype(daysold,'N') then call error
  93.  
  94.  too_old=date('b')-daysold
  95.  old_date=rxdates(too_old 'F')
  96.  
  97.  if right(targetdir,1)<>'\' then targetdir=targetdir || '\'
  98.  
  99.  say 'Scanning directory 'targetdir' for files older than 'old_date
  100.  call postlog 'Scanning directory 'targetdir' for files older than 'old_date
  101.  rc=sysfiletree(targetdir || '*',kill_list,'F')
  102.   if kill_list.0=0 then return
  103.    do i=1 to kill_list.0
  104.     parse var kill_list.i fdate ftime fsize fflags ffile .
  105.     fdate=translate(fdate,'/',':')
  106.     if too_old > Rxdates(fdate 'B') then
  107.      do
  108.       call postlog 'Deleting 'ffile' because it is older than 'rxdates(too_old 'U')
  109.       call sysfiledelete(ffile)
  110.      end /* do */
  111.    end /* do */
  112.  
  113.  return
  114.  
  115. error:
  116.     do  /* neither a listfile or a directory - exit */
  117.      say  ''
  118.      say
  119.      say  ' ***CAUTION*** ***ATTENTION*** ***WARNING***                             '
  120.      say
  121.      say  ' THIS PROGRAM IS ACTUALLY DESIGNED TO ERASE FILES FROM YOUR SYSTEM ON    '
  122.      say  ' A CONTROLLED BASIS. IMPROPER OR CAUSAL USE OF THIS PROGRAM CAN REMOVE   '
  123.      say  ' FILES THAT YOU MAY HAVE INTENDED TO KEEP. MAKE SURE YOU KNOW EXACTLY    '
  124.      say  ' WHAT YOU ARE DOING WHEN YOU USE THIS PROGRAM.                           '
  125.      say
  126.      say  ' YOU HAVE BEEN WARNED.                                                   '
  127.      say
  128.      say  ' ***CAUTION*** ***ATTENTION*** ***WARNING***                             '
  129.      say
  130.      say
  131.      say ' Invalid parms. '
  132.      say
  133.      say ' This program will delete all files from a directory that are <age> days old. '
  134.      say
  135.      say ' Format is 'myname' <directory> <age>'
  136.      say '   or '
  137.      say '           'myname' <listfile> <age>'
  138.      say ' where <listfile> contains a list of directories and age.'
  139.      say
  140.      say  ''
  141.      call postlog myname 'Parameter Error.'
  142.      call directory(basedir)
  143.      exit
  144.     end /* do */
  145.  return
  146.  
  147.  
  148. /* test for the existance of a file - return true or false condition */
  149. exist: procedure
  150.  arg filenameexist
  151.  return \(stream(filenameexist,'c','query exists')='')
  152.  
  153. /* error trap for rexx failures */
  154. errortrap:
  155.  call postlog myname 'Code Error.'
  156.  call postlog 'ERROR in batch file at line 'sigl
  157.  call postlog condition('C') condition('D') condition('I')
  158.  exit
  159. return
  160.  
  161.