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

  1. /*****************************************************************************/
  2. /* LSDIR: Show directory structure, with file counts and space totals        */
  3. /*                                                                           */
  4. /* The purpose of the following REXX program is to list all subdirectories   */
  5. /* in a tree, together with the number of files each contains and the        */
  6. /* amount of disk space consumed by the files. If the directory has          */
  7. /* subdirectories, the cumulative totals for files and space in all          */
  8. /* subdirectories is also shown. Apart from any utility this program may     */
  9. /* have, it illustrates one way of representing tree structures in REXX.     */
  10. /* Specifically, a unique identifier analogous to a library dewey decimal    */
  11. /* is constructed for each subdirectory (tree node) as it is encountered.    */
  12. /* This identifier is then used as one part of a compound name that          */
  13. /* references various quantities associated with each node.                  */
  14. /*                                                                           */
  15. /* Requires Personal REXX or REXXLIB (doscd, cursor, parsefn, dosdrive,      */
  16. /* dosdisk, dosdir, dosdirpos, scrput functions).                            */
  17. /*                                                                           */
  18. /* Command format: LSDIR <directory>                                         */
  19. /*                                                                           */
  20. /*****************************************************************************/
  21.  
  22. arg dir .
  23. if dir = '' then
  24.     dir = doscd()
  25. parse value cursor() with row col
  26. col = 1
  27. disk = word(parsefn(dir),1)
  28. if disk = '-' then
  29.     disk = dosdrive()
  30. sectors = dosdisk('s',disk)
  31. round = sectors * 512
  32.  
  33. tree. = 0
  34. tree..dirs = 1
  35. tree..name = 'top'
  36. prefix = ''
  37.  
  38. call bldtree dir, prefix
  39. call scrput row, col, copies(' ',80)
  40. call showtree '', 0, 1
  41. return
  42.  
  43. /* build the data structure */
  44. bldtree: procedure expose tree. row col round
  45. arg dir, prefix
  46. path = prefix'.'tree.prefix.dirs
  47. tree.path.dirs = 0
  48. tree.path.files = 0
  49. tree.path.bytes = 0
  50. tree.path.name = dir
  51. tree.path.totfiles = 0
  52. tree.path.totbytes = 0
  53. call scrput row, col, left('Reading:' dir,80)
  54. if word(parsefn(dir),2) \= '\' then
  55.     dir = dir||'\'
  56. line = dosdir(dir'*.*','san','hsd')
  57. do while line \= ''
  58.     position = dosdirpos();
  59.     parse var line size attr name
  60.     if name \= '.' & name \= '..' then
  61.         if pos('D',attr) \= 0 then do
  62.             tree.path.dirs = tree.path.dirs + 1
  63.             call bldtree dir||name, path
  64.             end
  65.         else do
  66.             tree.path.files = tree.path.files + 1
  67.             size = ((size + round - 1) % round) * round
  68.             tree.path.bytes = tree.path.bytes + size
  69.             end
  70.     line = dosdir(,'san','hsd',,position)
  71.     end
  72. tree.path.totfiles = tree.path.totfiles + tree.path.files
  73. tree.path.totbytes = tree.path.totbytes + tree.path.bytes
  74. tree.prefix.totfiles = tree.prefix.totfiles + tree.path.totfiles
  75. tree.prefix.totbytes = tree.prefix.totbytes + tree.path.totbytes
  76. return
  77.  
  78. /* display the tree */
  79. showtree: procedure expose tree.
  80. arg prefix, level, number
  81. path = prefix'.'number
  82. if level = 0 then
  83.     name = tree.path.name
  84. else do
  85.     i = lastpos('\',tree.path.name)
  86.     name = left(substr(tree.path.name,i+1),8)
  87.     end
  88. size = (tree.path.bytes + 1023) % 1024
  89. totsize = (tree.path.totbytes + 1023) % 1024
  90. line = copies('   ',level)||name right(tree.path.files,3) right(size||'K',6)
  91. if tree.path.dirs \= 0 then
  92.     line = line right(tree.path.totfiles,5) right(totsize||'K',6)
  93. say line
  94. do i = 1 to tree.path.dirs
  95.     call showtree path, level+1, i
  96.     end
  97. return
  98.