home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netdor2.zip / DISK_10 / IMAGE9.ZIP / DELDIR.CMD < prev    next >
OS/2 REXX Batch file  |  1993-08-17  |  6KB  |  183 lines

  1. /*****************************************************************************
  2.  *                      DELDIR - Delete Directory Tree                       *
  3.  *                       T. Bridgman (CORE at WATSON)                        *
  4.  *        Copyright (c) IBM Corporation, 1992.  All rights reserved.         *
  5.  *****************************************************************************
  6.  *                    Licensed Materials-Property of IBM                     *
  7.  *               5604-472 (c) Copyright IBM Corporation, 1993                *
  8.  *                           All rights reserved.                            *
  9.  *                  US Government Users Restricted Rights -                  *
  10.  *                 Use, duplication or disclosure restricted                 *
  11.  *                by GSA ADP Schedule Contract with IBM Corp.                *
  12.  *****************************************************************************
  13.  * REQUIRES:  RXUTILS.DLL, DATECONV.CMD                                      *
  14.  *****************************************************************************
  15.  * Version 1.0 - 6 Jan 92                                                    *
  16.  * 7 Jan 91 - teb                                                            *
  17.  * - Add /R option to not delete root directory of tree after emptying.      *
  18.  * - Properly qualify relative directory names (C:\.\.).                     *
  19.  * 4 May 92 - teb                                                            *
  20.  * - Delete empty directories when /D specified.                             *
  21.  * - Clear attributes of files.                                              *
  22.  * - Scan root of tree when /D specified.                                    *
  23.  * 13 Nov 92 - teb                                                           *
  24.  * - Handle directories with spaces in the name.                             *
  25.  * 10 Aug 93 - teb                                                           *
  26.  * - Change Q options to S.                                                  *
  27.  *****************************************************************************/
  28. trace 'O'
  29. parse upper arg Dir '/' Opts
  30. if Dir = '?' | Dir = ''
  31.   then signal Tell
  32.  
  33. call rxfuncadd 'RXLOADFUNCS', 'RXUTILS', 'RXLOADFUNCS'
  34. call RxLoadFuncs 
  35.  
  36. say
  37. Dir = ParseOpts(Dir, Opts)
  38.  
  39. say 'Delete directory tree:' Dir
  40. if Opt.!Query
  41.   then do
  42.     call rxSay 'Shall we continue? '
  43.     do until wordpos(Ch, 'Y N') <> 0
  44.       Ch = translate(rxGetKey('NOECHO'))
  45.     end
  46.     say Ch
  47.     if Ch = 'N'
  48.       then exit 2
  49.   end
  50. if right(Dir, 1) = '\'
  51.   then Dir = left(Dir, length(Dir)-1)
  52.  
  53. if Opt.!Age <= 0
  54.   then call DelTree Dir
  55.   else do
  56.     if Opt.!Dir
  57.       then rc = rxTree(Dir'\*', 'DATA.',  'BT',,'-*---')
  58.       else rc = rxTree(Dir'\*', 'DATA.',  'BST',,'-*---')
  59.     CutOff = dateconv(date('O'), 'O', 'O',, '-'Opt.!Age)'/00/00'
  60.     call rxStemSort 'DATA.', 'A', 1, 14
  61.     do I = 1 to Data.0 while word(Data.I, 1) < CutOff
  62.       Spec = subword(Data.I, 4)
  63.       IsDir = (pos('D', word(Data.I, 3)) > 0)
  64.       if IsDir
  65.         then if Opt.!Dir
  66.           then do
  67.             say '*** Deleting directory tree' Spec':'
  68.             call DelTree Spec
  69.             call RmDir Spec
  70.           end
  71.           else call RmDir Spec
  72.         else call DelFile Spec
  73.     end
  74.   end
  75. exit 0
  76.  
  77. DelTree: procedure expose Opt.
  78. parse arg Dir
  79. if right(Dir, 1) = '\'
  80.   then Dir = left(Dir, length(Dir)-1)
  81. rc = rxTree(Dir'\*', 'FILES.',  'BS',,'-*----')
  82. do I = Files.0 to 1 by -1
  83.   parse var Files.I . . . Flags File
  84.   File = strip(File)
  85.   if pos('EA DATA. SF', translate(File)) > 0
  86.     then if Opt.!FMsg
  87.       then say File '... skipped'
  88.       else nop
  89.     else if pos('D', Flags) > 0
  90.       then call RmDir File
  91.       else call DelFile File
  92. end
  93. if Opt.!DelRoot
  94.   then call RmDir Dir
  95. return 0
  96.  
  97. DelFile: procedure expose Opt.
  98. parse arg FName
  99. rc = rxDelete(FName)
  100. if Opt.!FMsg
  101.   then say FName '... rc('rc')'
  102. return rc
  103.  
  104. RmDir: procedure expose Opt.
  105. parse arg DirName
  106. '@RD "'DirName'" > NUL 2>&1'
  107. if Opt.!DMsg
  108.   then say DirName '... ('rc')'
  109. return rc
  110.  
  111. ParseOpts: procedure expose Opt.
  112. parse arg Dir, Opts
  113. parse value '1 0 0 1 1' with Opt.!Query Opt.!Age Opt.!Dir Opt.!FMsg,
  114.     Opt.!DMsg
  115. Dir = strip(Dir)
  116. Dir = strip(Dir,,'"')
  117. Home = directory()
  118. Test = directory(Dir)
  119. if Test = ''
  120.   then do
  121.     say 'Directory' Dir 'does not exist.'
  122.     exit 10
  123.   end
  124. Dir = Test
  125. Opt.!DelRoot = (length(Dir) > 3)
  126. call directory left(Dir, 3)
  127. call directory Home
  128. Opts = translate(Opts, '/', '-')
  129. if Opts <> ''
  130.   then do while Opts <> ''
  131.     parse var Opts Opt '/' Opts
  132.     parse var Opt Opt OptArg
  133.     select
  134.       when Opt = 'B'
  135.         then Opt.!Query = 0
  136.       when abbrev('AGE', Opt)
  137.         then Opt.!Age = OptArg
  138.       when abbrev('DIR', Opt)
  139.         then Opt.!Dir = 1
  140.       when Opt = 'S' | Opt = 'Q'
  141.         then Opt.!FMsg = 0
  142.       when Opt = 'SS' | Opt = 'QQ'
  143.         then do
  144.           Opt.!FMsg = 0
  145.           Opt.!DMsg = 0
  146.         end
  147.       when Opt = 'R'
  148.         then Opt.!DelRoot = 0
  149.       otherwise do
  150.         say 'Unrecognized option' Opt'.'
  151.         exit 10
  152.       end
  153.     end
  154.   end
  155. return Dir
  156.  
  157. Tell:
  158. say
  159. say 'DELDIR - Delete a complete directory tree'
  160. say
  161. say 'Syntax:  DELDIR directory [/B] [/Age <days> [/Dir]] [/Q[Q]] [/R]'
  162. say
  163. say 'Delete all files and subdirectories under the specified directory.'
  164. say
  165. say 'Options (which may be entered in any order):'
  166. say '  /B'
  167. say '    - Bypass the "are you sure" query -- dangerous!'
  168. say '  /Age <days> [/Dir]'
  169. say '    - Only delete files that are older than the specified number of days.'
  170. say '      This option will slow down processing considerably.'
  171. say '    - If /D is specified, all directories *immediately* off of the'
  172. say '      specified target that were created more than the specified'
  173. say '      number of days will be completely deleted, even if they contain'
  174. say '      files or directories that are newer than the cutoff date.'
  175. say '  /S[S]'
  176. say '    - If /S is specified, no deletion message is given for each file.'
  177. say '    - If /SS is specified, messages for directories are supressed'
  178. say '      as well.'
  179. say '  /R'
  180. say '    - Don''t delete the specified directory after emptying it.  (Assumed'
  181. say '      when the root directory is specified.'
  182. exit 0
  183.