home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 May / PCWK5A99.ISO / Os2 / STREE117.ZIP / showtree / showtree.cmd < prev   
Encoding:
Text File  |  1998-10-08  |  7.0 KB  |  285 lines

  1. /* showtree.cmd,v 1.17 1998-10-08 11:04:29-04 rl Exp */
  2.  
  3. /************************************************************************* 
  4.  *                                                                       * 
  5.  * showtree.cmd                                                          * 
  6.  * A REXX script to print a directory tree                               * 
  7.  * 1997-02-01, Rolf Lochbuehler <rolobue@ibm.net>                        * 
  8.  * 1998-10-02, Rolf Lochbuehler <rolf@together.net>                      * 
  9.  *                                                                       * 
  10.  *************************************************************************/
  11.  
  12. INDENT = ''
  13.  
  14. if 0 <> rxfuncquery('sysfiletree') then
  15.   call rxfuncadd 'sysfiletree', 'rexxutil', 'SysFileTree'
  16.  
  17. parse arg opt
  18.  
  19. if length(opt) = 0 then
  20.   do
  21.  
  22.   startdir = normalize(directory())
  23.   needhelp = 0
  24.   printascii = 0
  25.   maxlevel = 1000
  26.   printfiles = 0
  27.  
  28.   end
  29. else
  30.   do
  31.  
  32.   opt1 = translate( opt )
  33.   
  34.   i = wordpos( '/H', opt1 )
  35.   if i = 0 then
  36.     i = wordpos( '-H', opt1 )
  37.   if i > 0 then
  38.     needhelp = 1
  39.   else
  40.     needhelp = 0
  41.   
  42.   i = wordpos( '/A', opt1 )
  43.   if i = 0 then
  44.     i = wordpos( '-A', opt1 )
  45.   if i > 0 then
  46.     do
  47.     printascii = 1
  48.     opt = delword( opt, i, 1 )
  49.     opt1 = translate( opt )
  50.     end
  51.   else
  52.     printascii = 0
  53.   
  54.   i = wordpos( '/D', opt1 )
  55.   if i = 0 then
  56.     i = wordpos( '-D', opt1 )
  57.   if i > 0 then
  58.     do
  59.     maxlevel = word( opt1, i + 1 )
  60.     if maxlevel < 0 then
  61.       do
  62.       say 'Error: Negative level specified'
  63.       call help
  64.       exit 1
  65.       end
  66.     opt = delword( opt, i, 2 )
  67.     opt1 = translate( opt )
  68.     end
  69.   else
  70.     maxlevel = 1000
  71.   
  72.   i = wordpos( '/F', opt1 )
  73.   if i = 0 then
  74.     i = wordpos( '-F', opt1 )
  75.   if i > 0 then
  76.     do
  77.     printfiles = 1
  78.     opt = delword( opt, i, 1 )
  79.     opt1 = translate( opt )
  80.     end
  81.   else
  82.     printfiles = 0
  83.   
  84.   if length(opt) > 0 then
  85.     startdir = normalize(opt)
  86.   else
  87.     startdir = normalize(directory())
  88.  
  89.   end   /* end else */
  90.  
  91. if 1 = needhelp then
  92.   do
  93.   call help
  94.   exit 1
  95.   end
  96.  
  97. if 1 = printascii then
  98.   do
  99.   DWNCHR = '|'
  100.   MIDCHR = '|--'
  101.   ENDCHR = '`--'
  102.   end
  103. else
  104.   do
  105.   DWNCHR = d2c(179)                    
  106.   MIDCHR = d2c(195)||d2c(196)||d2c(196)
  107.   ENDCHR = d2c(192)||d2c(196)||d2c(196)
  108.   end
  109.   
  110. calldir = directory()
  111.  
  112. if translate(startdir) <> translate(directory(startdir)) then
  113.   do
  114.   say 'Error: cannot find' startdir
  115.   call help
  116.   exit 1
  117.   end
  118.  
  119. if 0 = printfiles then
  120.   say startdir
  121. else 
  122.   do
  123.   if lastpos('\',startdir) \= length(startdir) then
  124.     say startdir'\'
  125.   else
  126.     say startdir
  127.   end
  128.  
  129. level.0 = 1
  130. level.1 = 0
  131.  
  132. call tree
  133.  
  134. call directory calldir
  135. exit 0
  136.  
  137.  
  138. /************************************************************************* 
  139.  *                                                                       * 
  140.  * help()                                                                * 
  141.  * Display help screen for the user                                      * 
  142.  *                                                                       * 
  143.  *************************************************************************/
  144. help: procedure
  145.   say ''
  146.   say 'ShowTree V1.17 (REXX), Rolf Lochbuehler <rolf@together.net>'
  147.   say 'Purpose:'
  148.   say '  Print a directory tree in a command line window'
  149.   say 'Usage:'
  150.   say '  showtree [/a] [/d N] [/f] [/h] [Dir]'
  151.   say 'Arguments:'
  152.   say '  /a     Use ASCII characters only (default: PC850)'
  153.   say '  /d N   Show only N subdirectories below the root directory'
  154.   say '  /f     Print file names too (default: only directories)'
  155.   say '  /h     Print help, then exit'
  156.   say '  Dir    The root directory of the directory tree to print (default: .)'
  157.   say 'Examples:'
  158.   say '  showtree /h        prints help'
  159.   say '  showtree           starts in current directory using PC850 characters'
  160.   say '  showtree /a \bin   starts in \bin using ASCII characters' 
  161.   return
  162.  
  163.  
  164. /************************************************************************* 
  165.  *                                                                       * 
  166.  * tree()                                                                * 
  167.  * Recursive procedure to print directory tree                           * 
  168.  *                                                                       * 
  169.  *************************************************************************/
  170. tree: procedure expose INDENT DWNCHR MIDCHR ENDCHR level. maxlevel printfiles
  171.  
  172.   if 1 = printfiles then
  173.     call sysfiletree '*', 'entry', 'bo'
  174.   else
  175.     call sysfiletree '*', 'entry', 'do'
  176.  
  177.   if entry.0 > 0 then 
  178.     do i = 1 to entry.0
  179.  
  180.       isdir = 1
  181.       name = filespec( 'name', entry.i )
  182.       if (translate(name) = 'WP ROOT. SF') | (translate(name) = 'EA DATA. SF') then
  183.         isdir = 0
  184.       if (1 = printfiles) & ('' <> stream(entry.i,'command','query exists')) then
  185.         isdir = 0
  186.  
  187.       ln = INDENT
  188.  
  189.       do k = 1 to level.0 - 1
  190.         if level.k = 1 then
  191.           ln = ln'   'INDENT
  192.         else
  193.           ln = ln || DWNCHR'  'INDENT
  194.       end
  195.  
  196.       if i = entry.0 then
  197.         do
  198.         ln = ln || ENDCHR
  199.         m = level.0
  200.         level.m = 1
  201.         end
  202.       else                 
  203.         ln = ln || MIDCHR
  204.  
  205.       if (1 = printfiles) & (1 = isdir) then
  206.         ln = ln || filespec( 'name', entry.i ) || '\'
  207.       else
  208.         ln = ln || filespec( 'name', entry.i )
  209.  
  210.       say ln
  211.  
  212.       if (level.0 < maxlevel) & (1 = isdir) then
  213.         do
  214.  
  215.         call directory entry.i
  216.  
  217.         level.0 = level.0 + 1
  218.         m = level.0
  219.         level.m = 0
  220.   
  221.         call tree
  222.   
  223.         drop level.m
  224.         level.0 = level.0 - 1
  225.   
  226.         call directory '..'
  227.  
  228.         end
  229.  
  230.     end   /* end do */
  231.  
  232.   return
  233.  
  234.  
  235. /************************************************************************* 
  236.  *                                                                       * 
  237.  * normalize()                                                           * 
  238.  * Complete directory name, substitute ellipses, etc.                    * 
  239.  *                                                                       * 
  240.  *************************************************************************/
  241. normalize: procedure
  242.  
  243.   parse arg d
  244.  
  245.   d = strip( d, 'both' )
  246.   d = strip( d, 'both', '"' )
  247.  
  248.   if d = '' then
  249.     dn = directory()
  250.   
  251.   /* '.' */
  252.   else if d = '.' then
  253.     dn = directory()
  254.   
  255.   /* '..' */
  256.   else if d = '..' then
  257.     do
  258.     curdir = directory()
  259.     call directory '..'
  260.     dn = directory()
  261.     call directory curdir
  262.     end
  263.   
  264.   /* '\any\dir\name' (absolute path, but missing 'drive:') */
  265.   else if substr(d,1,1) = '\' then
  266.     do
  267.     drive = filespec( 'drive', directory() )
  268.     dn = drive || d
  269.     end
  270.   
  271.   /* 'any\dir\name' (relative path) */
  272.   else if pos(':',d) = 0 then
  273.     do
  274.     temp = directory() 
  275.     if lastpos('\',temp) <> length(temp) then
  276.       temp = temp || '\'
  277.     dn = temp || d
  278.     end
  279.  
  280.   else
  281.     dn = d
  282.  
  283.   return dn
  284.  
  285.