home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / cmdpk164.zip / xdel.cmd < prev    next >
OS/2 REXX Batch file  |  1998-01-10  |  4KB  |  151 lines

  1. /*  This is CommandPak's xdel command.                       */
  2. /*  (w) 1997-98 Ulrich Möller                                */
  3.  
  4. /*  WARNING: This is not documented yet and not an official  */
  5. /*  part of CommandPak.                                      */
  6.  
  7. debug=0
  8.  
  9. unknownoptionMsg = 'Unknown option (-%a). Type "ln -h" for help.'
  10. interruptMsg = "rm was interrupted externally."
  11. noFilesMsg = "Error in rm: No files found to remove."
  12. rmDirMsg = "Removing directory %a"
  13. rmFileMsg = "Removing %a"
  14. rmFileQs = 'Delete "%a"? (Y)es (N)o (A)ll '
  15. rmSpecialQs = '"%a" is a %b file.'||nl||'Delete anyway? (Y)es (N)o (A)ll '
  16.  
  17. signal on halt; trace off
  18.  
  19. if RxFuncQuery("SysLoadFuncs") then do
  20.     call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  21.     call SysLoadFuncs
  22. end
  23.  
  24. parse arg args
  25.  
  26. if args = "" then do
  27.     'call xhelp xdel'
  28.     exit
  29. end
  30.  
  31. verbose = 0
  32. force = 0
  33. interactive = 0
  34. alwaysReplace = 0
  35. deldir = 0
  36.  
  37. files.0 = 0
  38. attr.0 = 0
  39.  
  40. do while (args \= "")
  41.     parse value args with opt1 args
  42.     if debug then Say "Parsing" opt1
  43.     if (substr(opt1, 1, 1)="-") | (substr(opt1, 1, 1)="/") then do
  44.         do optcount = 2 to length(opt1) by 1
  45.             optchar = substr(opt1, optcount, 1)
  46.             if debug then
  47.                 Say "Subparsing" optchar
  48.             select
  49.                 when (optchar="D") then do
  50.                     Say "Debug messages turned on."
  51.                     debug = 1
  52.                     verbose = 1
  53.                 end
  54.                 when (optchar="f") then
  55.                     force = 1
  56.                 when (optchar="d") then
  57.                     deldir = 1
  58.                 when (optchar="v") then
  59.                     verbose = 1
  60.                 when (optchar="i") then do
  61.                     interactive = 1
  62.                     verbose = 1
  63.                 end
  64.                 when (optchar="h") | (optchar="?") then do
  65.                     'call xhelp xdel'
  66.                     exit
  67.                 end
  68.             otherwise
  69.                 say strReplace(unknownoptionMsg, "%a", optchar)
  70.             end /* select */
  71.         end /* do */
  72.     end /* if */
  73.     else do
  74.         rc = SysFileTree(opt1, temp.)
  75.         old0 = files.0
  76.         do i=1 to temp.0
  77.                 i2 = i+old0
  78.                 parse var temp.i fdate ftime size attr.i2 files.i2
  79.         end
  80.         files.0 = files.0+temp.0
  81.     end /* else */
  82. end /* do while */
  83.  
  84. if files.0 = 0 then
  85.     say nofilesMsg
  86.  
  87. do i=1 to files.0
  88.     confirm = ""
  89.     OK = 1
  90.     if (pos('D', attr.i) > 0) then
  91.         if deldir then do
  92.             say strReplace(rmDirMsg, '%a', files.i)
  93.             'rd' files.i
  94.             OK = 0
  95.         end
  96.         else iterate
  97.     else do
  98.         if (pos('H', attr.i) > 0) then
  99.             confirm = "hidden"
  100.         if (pos('R', attr.i) > 0) then
  101.             confirm = confirm "read-only"
  102.         if (pos('S', attr.i) > 0) then
  103.             confirm = confirm "system"
  104.         if (confirm \= "") then do
  105.             OK = queryYN(strReplace(strReplace(rmSpecialQs, '%a', strip(files.i)), '%b', confirm))
  106.             'attrib -h -r -s' files.i
  107.         end
  108.         else
  109.             if interactive then
  110.                 OK = queryYN(strReplace(rmFileQs, '%a', strip(files.i)))
  111.     end
  112.     if OK then do
  113.         if verbose then
  114.             say strReplace(rmFileMsg, '%a', strip(files.i))
  115.         'del' files.i
  116.     end
  117. end
  118.  
  119. exit
  120.  
  121. strReplace:
  122.     /* syntax: result = strReplace(str, old, new) */
  123.     /* will replace a by b in oldstr */
  124.     parse arg str, old, new
  125.     p = pos(old, str)
  126.     if (p > 0) then
  127.         return left(str, p-1)||new||substr(str,p+length(old))
  128.     else
  129.         return str
  130.  
  131. queryYN:
  132.     if \alwaysReplace then do
  133.         call charout , arg(1)
  134.         key = ''
  135.         do until pos(key,"YNA") > 0
  136.            key = translate(SysGetKey("NOECHO"))
  137.         end /* do */
  138.         Say key
  139.         if (key = "A") then
  140.             alwaysReplace = 1
  141.      end
  142.      if (alwaysReplace) then key = "Y"
  143. return (translate(key) = "Y")
  144.  
  145. halt:
  146.     Say ""
  147.     Say interruptMsg
  148.     exit
  149.  
  150.  
  151.