home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / drtree.zip / DIRTREE.CMD < prev    next >
OS/2 REXX Batch file  |  1994-02-17  |  12KB  |  291 lines

  1. /*  DIRTREE R1.0 1/1/94 by Conrad W. Clark 70544,760 */
  2. /*  you are licensed to use, copy, and redistribute as long */
  3. /*  as the 1st 5 lines of this file remain as is, and you and */
  4. /*  subsequent licensees agree that this program is distributed */
  5. /*  as is, use and consequences are solely the responsibility of the user. */
  6. /*  rexx pgm to list dir or subdir structs */
  7. /*  [d:][path]dirtree[.cmd] filespec [sd|st|ss|sp|sn]  [nod|not|nos|noa] */
  8. /*  for example at the E:> dirtree e:\rxtste\*.*   */
  9. /*   optional arguments (in any order) to sort (major to minor) or shorten output */
  10. /*        sd - sort by file date             nod - do not display date */
  11. /*        st - sort by file creation time    not - do not display time */
  12. /*        ss - sort by file size             nos - do not display size */
  13. /*        sp - sort by name including drive and path */
  14. /*        sn - sort by name excluding drive and path */
  15. /*                                            noa - do not display attributes*/
  16. /*      dirtree ? displays online help */
  17.  
  18. filespec = ''           /* file specification to give to SysFileTree */
  19. sorttype = ''           /* type of sort, if any */
  20.  
  21. /*  register rexx functions */
  22. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs' /* register all utility functions */
  23. call SysLoadFuncs
  24.      /* get the  structure to display or redirect  - max 11 args */
  25. arg filespec varg
  26.  
  27. if filespec = '' then
  28. do
  29.  say 'ERROR, missing filespec.  DIRTREE ? for help'
  30.  exit
  31. end
  32.  
  33. starttime = time('R')           /* reset elapsed timer */
  34. if filespec = '?' then
  35. do
  36. say '  DIRTREE R1.0 1/1/94 by Conrad W. Clark 70544,760'
  37. say '  a rexx program to list files including files in subdirectories.'
  38. say ' '
  39. say '  [d:][path]dirtree[.cmd] filespec [sd|st|ss|sp|sn]  [nod|not|nos|noa]'
  40. say '  for example at the E:> dirtree e:\rxtste\*.*'
  41. say '   optional arguments (in any order) to sort (major to minor)  or shorten output'
  42. say '        sd - sort by file date             nod - do not display date'
  43. say '        st - sort by file creation time    not - do not display time'
  44. say '        ss - sort by file size             nos - do not display size'
  45. say '        sp - sort by name including        noa - do not display attributes'
  46. say '             and path'
  47. say '        sn - sort by name excluding drive and path'
  48. say '  '
  49. exit
  50. end
  51.  
  52. /* constants of valid arguments */
  53. vcon = 'SD' 'ST' 'SS' 'SP' 'SN' 'NOD' 'NOT' 'NOS' 'NOA'
  54. do i = 1 to words(varg)  /* set up argument and constant arrays */
  55.  varg.i = word(varg,i)
  56. end
  57. do j = 1 to words(vcon)
  58.   vcon.j = word(vcon,j)
  59. end 
  60. do i = 1 to words(varg)         /*  check arguments for validity */
  61.    foundflag = 0
  62.    do j = 1 to words(vcon)
  63.       if varg.i = vcon.j then
  64.         do
  65.            foundflag = 1        /* a valid argument */
  66.            leave j
  67.         end
  68.    end 
  69.       if foundflag = 0  then          /* not a valid argument */
  70.         do
  71.            say 'ERROR - invalid command line argument = ' varg.i
  72.            say 'DIRTREE ? for help '
  73.            exit
  74.         end
  75. end
  76.  
  77. say 'filespec is' filespec
  78. call SysFileTree filespec, 'files.', 'sf'    /* get files in  dir structure */
  79.                                      /* including subdirectories */
  80.  
  81. if result <> 0  then
  82. do
  83.         say 'source file ERROR, SysFileTree result = ' result
  84.         say 'result 2 is not enough memory'
  85.         exit
  86. end
  87.  
  88. if files.0 = 0 
  89.  then
  90.         do
  91.            say 'no files found'
  92.            exit
  93.         end
  94.  
  95. sorttype.0 = 0
  96. do isort = 1 to words(varg)             /*  get a sort(s) to do */
  97.   do jsort = 1 to 5
  98.     if varg.isort = vcon.jsort then 
  99.     do
  100.       sortflag = 1  /* we will call sortfiles */
  101.       sorttype.0 = sorttype.0 + 1
  102.       sorttypeindex = sorttype.0
  103.       sorttype.sorttypeindex = varg.isort  /* append another field to sort on */
  104. say 'sort on ' varg.isort
  105.     end
  106.   end
  107. end
  108.   
  109. if sortflag = 1 then call sortfiles      /* sort if asked to */
  110.                                            /* sortfiles uses files., keysize, keyposition */
  111.  
  112. nodflag = 0
  113. notflag = 0
  114. nosflag = 0
  115. noaflag = 0
  116. do i = 1 to words(varg)                    /*  see what to not print */
  117.    if varg.i = 'NOD' then nodflag = 1
  118.    if varg.i = 'NOT' then notflag = 1
  119.    if varg.i = 'NOS' then nosflag = 1
  120.    if varg.i = 'NOA' then noaflag = 1
  121. end
  122. do i = 1 to files.0                      /* output each file */
  123.    outstring = ''
  124.    outstringptr = 1                     /*  current position in output */
  125.                                   /*  move in stuff to display */
  126.    if nodflag = 0 then outstring = substr(files.i,1,10)    /* insert date */
  127.    if notflag = 0 then outstring = outstring||substr(files.i,11,8) /* insert time */
  128.    if nosflag = 0 then outstring = outstring||substr(files.i,19,12) /* insert size */
  129.    if noaflag = 0 then outstring = outstring||substr(files.i,31,7) /*  insert attributes */
  130.    outstring = outstring||substr(files.i,38)  /* always display path||filename */
  131.    call lineout 'stdout:', outstring
  132.    if result <> 0  then
  133.       do
  134.          say 'source file ERROR, lineout result = ' result
  135.          say 'check 3rd argument, printer status, file name'
  136.          exit
  137.       end
  138. end
  139. starttime = time('E')           /* running time */
  140. say 'files found = ' files.0
  141. say 'time to run dirtree = ' starttime
  142. exit             /* normal completion */
  143. /**********************/
  144. /*                           */
  145. /*  SORT ROUTINES       */
  146. /*                           */
  147. /**********************/
  148. sortfiles:                      /* shell sort of input files */
  149.                                 /* use shell sort (Sedgewick:Algorithms 1984) */
  150.                                 /* since many directories may be already ordered */
  151. h = 1
  152. do until h>files.0
  153.   h = 3*h+1
  154. end
  155. do forever until h = 1
  156.   h = h % 3               /* integer result */
  157.   do i = h+1 to files.0
  158.     v = files.i
  159.     j = i
  160.     k = j-h
  161.  
  162.                            /* assemble key1 */
  163.     key1 = ''
  164.     do sorttypeindex = 1 to sorttype.0
  165.      select                                 /* get key1 in outer loop */
  166.          when sorttype.sorttypeindex = 'SD' then        /* sort by date */
  167.             do
  168.                keysize = 8                /* mm/dd/yy is 8 bytes */
  169.                keyposition = 1            /* 1st field in files.i */
  170.                rawkey1 = substr(v,keyposition)  /* blanks to 0 in key */
  171.                do ikey = 1 to keysize         /* convert blanks to 0 */
  172.                   if substr(rawkey1,ikey,1) = ' ' then rawkey1 = overlay('0',rawkey1,ikey)
  173.                end
  174.                                         /* reorder date in key */
  175.                key1 = key1||substr(rawkey1,7,2)||'/'||substr(rawkey1,1,3)||substr(rawkey1,4,2)
  176.             end
  177.          when sorttype.sorttypeindex = 'ST' then        /* sort by time */
  178.             do
  179.                keysize = 6
  180.                keyposition = 11
  181.                rawkey1 = substr(v,keyposition)
  182.                do ikey = 1 to keysize         /* convert blanks to 0 */
  183.                   if substr(rawkey1,ikey,1) = ' ' then rawkey1 = overlay('0',rawkey1,ikey)
  184.                end
  185.                key1 = key1||rawkey1
  186.             end
  187.          when sorttype.sorttypeindex = 'SP' then        /* sort by d:\path[\path..]\name.ext */
  188.             do
  189.                keyposition = 38
  190.                key1 = key1||substr(v,keyposition)
  191.             end
  192.          when sorttype.sorttypeindex = 'SN' then        /* sort by name.ext */
  193.             do
  194.                key1position = lastpos('\',files.i) + 1    /* last \ is just before file name */
  195.                key1 = key1||substr(v,key1position)
  196.             end
  197.          when sorttype.sorttypeindex = 'SS' then        /* sort by file size */
  198.             do
  199.                keysize = 10
  200.                keyposition = 19
  201.                rawkey1 = substr(v,keyposition,keysize)
  202.                     do ikey = 1 to keysize         /* convert blanks to 0 */
  203.                         if substr(rawkey1,ikey,1) = ' ' then rawkey1 = overlay('0',rawkey1,ikey)
  204.                     end
  205.                key1 = key1||rawkey1
  206.             end
  207.  
  208.       otherwise
  209.             do
  210.                say 'ERROR in program logic sort key1'  /* no sort argument */
  211.                say ' key1, sorttype.sorttypeindex = ' key1 sorttype.sorttypeindex '.' sorttypeindex
  212.                exit
  213.             end
  214.       end  /* select */
  215.      end  /* do sorttypeindex */
  216.  
  217.      do forever
  218.      key2 = ''                             /* assemble key2 */
  219.       do sorttypeindex2 = 1 to sorttype.0
  220.        select                                 /* get key1 in outer loop */
  221.          when sorttype.sorttypeindex2 = 'SD' then        /* sort by date */
  222.             do
  223.                keysize = 8                /* mm/dd/yy is 8 bytes */
  224.                keyposition = 1            /* 1st field in files.i */
  225.                rawkey2 = substr(files.k,keyposition,keysize)  /* blanks to 0 in key */
  226.                do ikey = 1 to keysize         /* convert blanks to 0 */
  227.                   if substr(rawkey2,ikey,1) = ' ' then rawkey2 = overlay('0',rawkey2,ikey)
  228.                end
  229.                                         /* reorder date in key */
  230.                key2 = key2||substr(rawkey2,7,2)||'/'||substr(rawkey2,1,3)||substr(rawkey2,4,2)
  231.             end
  232.          when sorttype.sorttypeindex2 = 'ST' then        /* sort by time */
  233.             do
  234.                keysize = 6
  235.                keyposition = 11
  236.                rawkey2 = substr(files.k,keyposition,keysize)
  237.                do ikey = 1 to keysize         /* convert blanks to 0 */
  238.                   if substr(rawkey2,ikey,1) = ' ' then rawkey1 = overlay('0',rawkey1,ikey)
  239.                end
  240.                key2 = key2||rawkey2
  241.             end
  242.          when sorttype.sorttypeindex2 = 'SP' then        /* sort by d:\path[\path..]\name.ext */
  243.             do
  244.                keyposition = 38
  245.                rawkey2 = substr(files.k,keyposition)
  246.                key2 = key2||substr(files.k,keyposition)
  247.             end
  248.          when sorttype.sorttypeindex2 = 'SN' then        /* sort by name.ext */
  249.             do
  250.                key2position = lastpos('\',files.k) + 1    /* last \ is just before file name */
  251.                key2 = key2||substr(files.k,key2position)
  252.             end
  253.          when sorttype.sorttypeindex2 = 'SS' then        /* sort by file size */
  254.             do
  255.                keysize = 10
  256.                keyposition = 19
  257.                rawkey2 = substr(files.k,keyposition,keysize)
  258.                do ikey = 1 to keysize         /* convert blanks to 0 */
  259.                     if substr(rawkey2,ikey,1) = ' ' then rawkey2 = overlay('0',rawkey2,ikey)
  260.                end
  261.                key2 = key2||rawkey2
  262.             end
  263.  
  264.       otherwise
  265.             do
  266.                say 'ERROR in program logic sort key2'  /* no sort argument */
  267.                say ' key2, sorttype.sorttypeindex2 = ' key2 sorttype.sorttypeindex2 '.' sorttypeindex2
  268.                exit
  269.             end
  270.       end  /* select */
  271.      end  /* do sorttypeindex2 */
  272.  
  273.      if key2 <= key1 then leave     /* ending condition */
  274.      files.j = files.k
  275.      j = j-h
  276.      k = j-h
  277.      if j <= h then leave
  278.     end  /* inner do forever */
  279.   files.j = v
  280.   end /* do i = h1 to files.0 */
  281. end /* do forever until h = 1 */
  282. return
  283.  
  284. /*  words subroutine */
  285. /*  call words(inputlist) returns number of words in inputlist */
  286. iwords = 0
  287. do while word(inputlist,iwords) <> ''
  288.  iwords = iwords + 1
  289. end /* do */
  290. return iwords
  291.