home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rexxlb.zip / SAMPLES / BLAST.CMD < prev    next >
OS/2 REXX Batch file  |  1993-01-09  |  4KB  |  119 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* BLAST: obliterate a directory and all subdirectories. BLAST recursively   */
  4. /* deletes all files and subdirectories of a specified directory. The user   */
  5. /* is prompted if hidden, system, or read-only files are encountered.        */
  6. /*                                                                           */
  7. /* Requires Personal REXX or REXXLIB (parsefn, dosfname, dosisdir, dosdir,   */
  8. /* dosdirpos, doschmod, upper, lower, dosdel, dosrmdir, doscd functions).    */
  9. /* functions).                                                               */
  10. /*                                                                           */
  11. /* Command format: BLAST <directory>                                         */
  12. /*                                                                           */
  13. /*****************************************************************************/
  14.  
  15. parse arg target .
  16. if target = '' then do
  17.     say 'Enter name of directory:'
  18.     parse pull target
  19.     if target = '' then do
  20.         say 'You had second thoughts about it. OK.'
  21.         exit
  22.         end
  23.     end
  24.  
  25. say 'Warning!' target 'and all its subdirectories will be purged!'
  26. say 'OK to continue (y/n)?'
  27. parse upper pull ok
  28. if \abbrev('YES', ok, 1) then do
  29.     say target 'has been spared.'
  30.     exit
  31.     end
  32.  
  33. call blast target
  34. exit
  35.  
  36. /* recursive subroutine to delete directories */
  37. blast: procedure
  38. dirname = arg(1)
  39. parse value parsefn(dirname) with . . fn ft
  40. if fn \= '-' then do
  41.     fullname = dosfname(dirname)
  42.     if fullname = '' then do
  43.         say 'Invalid name:' dirname
  44.         return 1
  45.         end
  46.     end
  47. else
  48.     fullname = dirname
  49. if \dosisdir(fullname) then do
  50.     say 'Not a directory:' fullname
  51.     return 2
  52.     end
  53. searchid = make_name(fullname, '*.*')
  54.  
  55. /* first pass: get rid of all subdirectories */
  56. file = dosdir(searchid, 'na', 'dh', 'd')
  57. do while file \= ''
  58.     dirpos = dosdirpos()
  59.     parse var file subdir attr
  60.     if subdir \= '.' & subdir \= '..' then do
  61.         if pos('H', upper(attr)) \= 0 then do
  62.             say make_name(fullname, subdir) 'is a hidden directory.'
  63.             say 'OK to continue (y/n)?'
  64.             parse upper pull ok
  65.             if \abbrev('YES', ok, 1) then
  66.                 return 3
  67.             end
  68.         call blast make_name(fullname, subdir)
  69.         if result \= 0 then
  70.             return 4
  71.         end
  72.     file = dosdir(, 'na', 'dh', 'd', dirpos)
  73.     end
  74.  
  75. /* second pass: get rid of files */
  76. file = dosdir(searchid, 'na', 'sh')
  77. do while file \= ''
  78.     parse var file fn attr
  79.     fileid = make_name(fullname, fn)
  80.     if verify(upper(attr), 'HSR', 'm') \= 0 then do
  81.         say fileid 'has read-only, system, or hidden attribute.'
  82.         say 'OK to continue (y/n)?'
  83.         parse upper pull ok
  84.         if \abbrev('YES', ok, 1) then
  85.             return 5
  86.         call doschmod fileid, , 'hsr'
  87.         end
  88.     if dosdel(fileid) \= 1 then do
  89.         say 'Error deleting' fileid
  90.         return 6
  91.         end
  92.     file = dosdir(, 'na', 'sh')
  93.     end
  94.  
  95. /* finally, delete the directory */
  96. parse value parsefn(fullname) with disk dir . .
  97. if disk \= '-' then
  98.     curdir = doscd(disk)
  99. if dir = '\' then
  100.     return 0
  101. dir = strip(dir, 't', '\')
  102. if lower(curdir) = lower(dir) then
  103.     'cd' disk':..'
  104. call dosrmdir fullname
  105. if dosisdir(fullname) then do
  106.     say 'Error removing' fullname
  107.     return 7
  108.     end
  109. say fullname 'blasted.'
  110. return 0
  111.  
  112. /* make a name out of path and file id */
  113. make_name: procedure
  114. parse arg path, name
  115. if right(path, 1) = '\' | right(path, 1) = ':' then
  116.     return path||name
  117. else
  118.     return path'\'name
  119.