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

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* TREED: diagrams tree structure of a disk with subdirectories. Starts at   */
  4. /* root directory of current disk, or at directory given as argument.        */
  5. /*                                                                           */
  6. /* Requires Personal REXX or REXXLIB (dosdrive, doscd, dosdir, arraysort     */
  7. /* functions).                                                               */
  8. /*                                                                           */
  9. /* Command format: TREED <directory>                                         */
  10. /*                                                                           */
  11. /*****************************************************************************/
  12.  
  13. /* process parameter, if any, and check it names existing directory */
  14. base = strip(arg(1))
  15. name.0 = base
  16. location.0 = 1
  17. if right(base,1) = '\' then
  18.    base = left(base, length(base)-1)
  19. if base = '' then
  20.    base = dosdrive()':'
  21. badarg = 0
  22. if length(base) = 2 & right(base, 1) = ':' then do
  23.    if doscd(base) = '' then badarg = 1
  24.    end
  25. else do
  26.    if dosdir(base,,'d','d') = '' then
  27.       badarg = 1
  28.    end
  29. if badarg then
  30.    say "'"arg(1)"'" 'is not the name of a directory.'
  31. else
  32.    call tree base, 1   /* call tree routine to do the real work */
  33. exit
  34.  
  35. /* subroutine to recursively trace through directory tree structure */
  36. tree: procedure expose name. location. total.
  37.   arg directory, depth
  38.   n = 0
  39.   name = dosdir(directory'\*.*','n','d','d')
  40.   do while name \= ''
  41.     if name \= '.' & name \= '..' then do
  42.        n = n + 1
  43.        dir.n = name
  44.       end
  45.     name = dosdir(,'n','d','d')
  46.    end
  47.    call sortdir n
  48.    total.depth = n
  49.    do loc = 1 to n
  50.       name.depth = dir.loc
  51.       location.depth = loc
  52.       call tree directory'\'dir.loc, depth+1
  53.       call output
  54.      end
  55.    return
  56.  
  57. /* routine to output a line of the output diagram */
  58. output:
  59.    if name.depth = '' then return
  60.    line = ''
  61.    do i = 1 to depth
  62.         select
  63.          when i < depth & name.i = '' then
  64.             if location.i = total.i then
  65.                join = ''
  66.             else
  67.                join = d2c(179)
  68.          when total.i = 1 & name.i \= '' then join = d2c(196)
  69.          when location.i = 1 then join = d2c(194)
  70.          when location.i = total.i then join = d2c(192)
  71.          otherwise join = d2c(195)
  72.          end
  73.       line = left(line,(i-1)*11) || join
  74.       if name.i \= '' then do
  75.          line = left(line,(i-1)*11) || join || d2c(196) || name.i
  76.          if i < depth then
  77.             line = left(line,i*11,d2c(196))
  78.          end
  79.       name.i = ''
  80.       end
  81.    if location.0 = 1 then do
  82.       say name.0 || d2c(196) || line
  83.       location.0 = 0
  84.       end
  85.    else
  86.       say copies(' ',length(name.0)+1) || line
  87.    return
  88.  
  89. /* sort a list of subdirectories into alphabetical order */
  90. sortdir: procedure expose dir.
  91.    parse arg n
  92.    if n > 0 then
  93.       call arraysort 'dir.', 1, n, 1, 256, 'a'
  94.    return
  95.