home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / D2.LZH / d.cmd next >
OS/2 REXX Batch file  |  1993-04-13  |  15KB  |  374 lines

  1. /**************************************************************
  2.  *           D type file listing for OS/2                     *
  3.  *                                                            *
  4.  *Copyright 1993 C.E.Gaumer Software                          *
  5.  *                                                            *
  6.  *Type D/? for help                                           *
  7.  *                                                            *
  8.  *If you make any useful modifications to this program        *
  9.  *  please send a copy of the modified program to me          *
  10.  *  I would be most interested in seeing your mods            *
  11.  *  address is on the help screen                             *
  12.  *                                                            *
  13.  *                                                            *
  14.  *                                                            *
  15.  *                                                            *
  16.  *                                                            *
  17.  *                                                            *
  18.  *                                                            *
  19.  *                                                            *
  20.  *                                                            *
  21.  *defswitch can be set for default switches for example       *
  22.  *defswitch='/h' will show hidden files by default            *
  23.  **************************************************************/
  24.  
  25. defswitch=''
  26. call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  27. call SysLoadFuncs
  28. attribs='**-**'
  29. switch=''
  30. argument=STRIP(arg(1))                                /*remove lead/trail */
  31.  
  32. if length(argument)>0 then do                         /*if arg supplied   */
  33.    if POS('/',argument)>0 then do                     /*if switch supplied*/
  34.       switch=SUBSTR(argument,POS('/',argument))       /*parse switch(es)  */
  35.  
  36.       argument=SUBSTR(argument,1,(POS('/',argument)-1))
  37.                                                       /*remove switch(es) */
  38.       fspec=STRIP(argument)                           /*remove lead/trail */
  39.    end /* do */
  40.    else fspec=argument                                /*if no switches    */
  41.    if POS('?',switch)>0 then call help                /*does he want help?*/
  42.    if length(argument)=0 then fspec='*.*'             /*if no filespec    */
  43.    PARSE UPPER VAR switch switch                      /*upper case switch */
  44.  
  45.    fspec=STRIP(fspec,,' ')                            /*remove lead/trail */
  46.    fspec=STRIP(fspec,,'"')                            /*remove quotes     */
  47.  
  48.    if(LASTPOS(':',fspec)=LENGTH(fspec))|(LASTPOS('\',fspec)=LENGTH(fspec)) then
  49.       fspec=fspec||'*.*'
  50.    else do
  51.       call SysFileTree fspec,junk,'D'                 /*check if directory*/
  52.       if(LASTPOS('*',fspec)=0 & LASTPOS('?',fspec)=0 & junk.0=1) then
  53.       fspec=fspec||'\*.*'                             /*append if dir and */
  54.    end
  55. end                                                   /* no wildcards     */
  56. else fspec='*.*'                                      /* if all else fails*/
  57. if LENGTH(switch)=0 then PARSE UPPER VAR defswitch switch
  58. if POS('H',switch)>0 then attribs='*****'
  59.  
  60. rc=SysFileTree(fspec,'file','F',attribs)              /* get filenames    */
  61. filelen=12                                            /* minimum length   */
  62. do i=1 to file.0
  63.    temp.i=SUBSTR(file.i,LASTPOS('\',file.i)+1)
  64.    if length(temp.i) > filelen then                   /* find max file    */
  65.    filelen = length(temp.i)                           /*  name length     */
  66. end /* do */
  67. temp=SUBSTR(fspec,1,LASTPOS('\',fspec))               /* path only        */
  68. if LENGTH(temp)=0 then temp=SUBSTR(fspec,1,LASTPOS(':',fspec))
  69. temp=temp||'*.*'                                      /* all directories  */
  70. if POS('D',switch)=0 then temp=fspec                  /*just matched dirs */
  71. call SysFileTree temp ,'dfile','D'                    /* get directories  */
  72. do i=1 to dfile.0                                     /*sift thru them    */
  73.    temp.i=SUBSTR(dfile.i,LASTPOS('\',dfile.i)+1)
  74.    if length(temp.i) > filelen then                   /* find max file    */
  75.    filelen = length(temp.i)                           /*  name length     */
  76. end /* do */
  77. do i=1 to file.0
  78.    size.i=SUBSTR(file.i,18,11)                        /* parse out size   */
  79.    file.i=SUBSTR(file.i,LASTPOS('\',file.i)+1,filelen,' ') /* and name    */
  80. end /* do */                                          /*  of files        */
  81.  
  82. do i=1 to dfile.0
  83.    dsize.i='     <DIR> '                              /* make size <dir>  */
  84.    dfile.i=SUBSTR(dfile.i,LASTPOS('\',dfile.i)+1,filelen,' ')
  85.    if POS('S',switch)>0 then do                       /* we want subs     */
  86.       temp=fspec
  87.       if LASTPOS('\',temp)=0 then                     /*no '\' in fspec   */
  88.          temp=INSERT(STRIP(dfile.i)||'\',temp,LASTPOS(':',temp))
  89.       else
  90.          temp=INSERT(STRIP(dfile.i)||'\',temp,LASTPOS('\',temp))
  91.       rc=SysFileTree(temp,'subfile','FS')             /* get subdir files */
  92.       dsize.i=0
  93.       if subfile.0>0 then                             /* files exist      */
  94.       do n=1 to subfile.0
  95.          filesize= (SUBSTR(subfile.n,18,11))          /* get file size    */
  96.  
  97.          dsize.i=dsize.i+filesize                     /* add to total     */
  98.       end                                             /*  for this dir    */
  99.       dsize2.i=dsize.i                                /*save size for sort*/
  100.       do n=LENGTH(dsize.i) to 8                       /*pad the one we'll */
  101.          dsize.i=INSERT(' ',dsize.i,0)                /* use to print     */
  102.       end
  103.       dsize.i=INSERT('<',dsize.i,0)                   /* put the angle    */
  104.       dsize.i=dsize.i||'>'                            /*brackets in place */
  105.    end
  106. end /* do */
  107.  
  108. call sort                                             /*call the sort func*/
  109.  
  110.  
  111. line=2                                                /* init line count  */
  112. list.1='┌'                                            /*build top line    */
  113. do i=1 to filelen+12
  114.    list.1=list.1||'─'
  115. end
  116.  
  117. do while length(list.1) < (79-(filelen+11)) then
  118.    list.1=list.1||'┬'
  119.    do i=1 to filelen+12
  120.      list.1=list.1||'─'
  121.    end
  122. end
  123.  
  124. list.1=list.1||'┐'
  125. line=2
  126. if dfile.0>0 then                                     /* if dirs  exist   */
  127. do
  128.    list.line='│'                                      /* left  border     */
  129.    do i=1 to dfile.0
  130.       if length(list.line)+filelen+12 > 80 then       /* is this line     */
  131.       do                                              /*           full ? */
  132.         line=line+1                                   /* next line        */
  133.         list.line='│'                                 /* left  border     */
  134.       end
  135.       list.line=list.line||' '||dfile.i||dsize.i||'│' /*put dfile,size,sep*/
  136.    end
  137.    i=line
  138.    do while length(list.i) < (79-(filelen+11))        /*fill separators   */
  139.       do n=1 to filelen+12                            /* when no more     */
  140.          list.i=list.i||' '                           /*  dirs  left      */
  141.       end
  142.       list.i=list.i||'│'
  143.    end
  144. line=line+1
  145. end
  146. if file.0>0 then
  147. do
  148.    list.line='│'                                       /* left  border     */
  149.    do i=1 to file.0
  150.       if length(list.line)+filelen+12 > 80             /* is this line     */
  151.          then do                                       /*           full ? */
  152.             line=line+1                                /* next line        */
  153.             list.line='│'                              /* left  border     */
  154.          end
  155.       list.line=list.line||' '||file.i||size.i||'│'    /*put file,size,sep */
  156.    end
  157.  
  158.    i=line
  159.    do while length(list.i) < (79-(filelen+11))         /*fill separators   */
  160.       do n=1 to filelen+12                             /* when no more     */
  161.         list.i=list.i||' '                             /*  files left      */
  162.       end
  163.    list.i=list.i||'│'
  164.    end
  165.  
  166. line=line+1                                            /* increment line   */
  167. end                                                    /*end of if files   */
  168. list.line='└───'                                       /* start bottom     */
  169. list.line=list.line||'[ dir '||fspec||' ]'             /* with filespec    */
  170. next= (filelen+12)                                     /* where does next  */
  171. do while next<length(list.line)                        /* 'T' character go?*/
  172.    next=next+filelen+13
  173. end
  174. x= length(list.line)
  175. do i=x to next                                        /* fill to there    */
  176.    list.line=list.line||'─'
  177. end
  178. do while length(list.line) < (79-(filelen+11)) then   /* build rest of    */
  179.    list.line=list.line||'┴'                           /*  bottom border   */
  180.    do i=1 to filelen+12
  181.      list.line=list.line||'─'
  182.    end
  183. end
  184. list.line=list.line||'┘'                              /*finish bottom bor */
  185. line=line+1
  186. list.line=' '||dfile.0|| '~dir '
  187. list.line=list.line||file.0|| '~fil '
  188.  
  189.  
  190.  
  191. totsize=0
  192. do i=1 to file.0
  193.    totsize=totsize+size.i                             /*add file sizes    */
  194. end
  195.    if POS('S',switch)>0 then                          /*if subs selected  */
  196. do i=1 to dfile.0
  197.    totsize=totsize+dsize2.i                           /*add dir sizes     */
  198. end
  199.  
  200.  
  201.  
  202. drive=LEFT(fspec,2)                                   /*split 1st 2 chars */
  203. if LASTPOS(':',drive)\=2 then do                  /*not valid drive spec? */
  204.    direct=DIRECTORY()                                 /*then use current  */
  205.    drive=LEFT(direct,2)                               /*split 1st 2 chars */
  206. end
  207.  
  208. info=SysDriveInfo(drive)                              /*get drive info    */
  209. freespace=WORD(info,2)
  210. totspace=WORD(info,3)                                 /*parse drive info  */
  211. usedspace=totspace-freespace
  212. filesbar=0
  213. if usedspace>0 then
  214.    filesbar=TRUNC(totsize/totspace*80)               /* calc bar graph   */
  215. usedbar=TRUNC((usedspace/totspace*80)-filesbar)       /* sizes            */
  216.  
  217.  
  218. totsize= REVERSE(totsize)
  219. n=(LENGTH(totsize)-1)/3
  220. commaspace=3
  221. do i=1 to n
  222.    totsize= INSERT(',',totsize,commaspace)            /*build commas into */
  223.    commaspace=commaspace+4                            /*numbers for disp  */
  224. end
  225. totsize= REVERSE(totsize)
  226. list.line=list.line||totsize||'byt(█) '
  227.  
  228. usedspace= REVERSE(usedspace)
  229. n=(LENGTH(usedspace)-1)/3
  230. commaspace=3
  231. do i=1 to n
  232.    usedspace= INSERT(',',usedspace,commaspace)
  233.    commaspace=commaspace+4
  234. end
  235. usedspace= REVERSE(usedspace)
  236.  
  237. freespace= REVERSE(freespace)
  238. n=(LENGTH(freespace)-1)/3
  239. commaspace=3
  240. do i=1 to n
  241.    freespace= INSERT(',',freespace,commaspace)
  242.    commaspace=commaspace+4
  243. end
  244. freespace= REVERSE(freespace)
  245.                                                     /*build last text line*/
  246. list.line=list.line||usedspace||'byt~used(▄) '||freespace||'byt~free(_)'
  247. temp=SPACE(list.line,(TRUNC((79-LENGTH(list.line))/4)))/*let REXX space it*/
  248. list.line=' '
  249. do i=1 to LENGTH(temp)/*then replace the '~' we used to make words w/spaces*/
  250.   if(SUBSTR(temp,i,1))='~' then list.line=list.line||' '
  251.   else list.line=list.line||SUBSTR(temp,i,1)
  252. end
  253.  
  254. line=line+1
  255. list.line=''
  256. if filesbar>0 then
  257.    do i=1 to filesbar
  258.       list.line=list.line||'█'
  259.    end                                      /* build bargraph  */
  260. if usedbar>0 then
  261.    do i=1 to usedbar
  262.       list.line=list.line||'▄'
  263.    end
  264. if LENGTH(list.line)<79 then
  265.    do i=LENGTH(list.line) to 79
  266.       list.line=list.line||'_'
  267.    end
  268.  
  269.  
  270. /* All done building printable list so let's print it */
  271.  
  272. parse value SysTextScreenSize() with vrows col        /*how big is screen?*/
  273. list.0=line
  274. do i=1 to list.0
  275.    if i//(vrows-2)=0&POS('N',switch)=0 then           /* if screen is full*/
  276.                                                       /* and pause not off*/
  277.    call wait                                          /* call'MORE'routine*/
  278.    say list.i                                         /* print line       */
  279. end
  280. call SysDropFuncs                                     /*  cleanup         */
  281. EXIT                                                  /* and go home!!    */
  282.  
  283.  
  284. /********************WAIT FUNCTION FOR "MORE..." PROMPT*******************/
  285. wait:
  286. parse value SysCurPos() with row col                  /*where is cursor?  */
  287. row=row-1                                             /*we will scroll 1  */
  288. say More...                                           /* with this...     */
  289. call SysGetKey(NOECHO)                                /* get a key        */
  290. call SysCurPos row,col                                /* put cursor back  */
  291. return                                                /* go back          */
  292.  
  293. /******************SORT FUNCTION ***************************************/
  294. sort:
  295. sort.0=file.0
  296. do i=1 to file.0
  297.    if POS('C',switch)=0 then PARSE UPPER VAR file.i sort.i
  298.    else sort.i=file.i
  299. end
  300.  
  301.  
  302. do until done=1
  303.    done=1
  304.    do i=1 to (file.0-1)
  305.       n=i+1
  306.       if sort.i>sort.n then do
  307.          temp=file.i
  308.          stemp=size.i
  309.          sstemp=sort.i
  310.          file.i=file.n
  311.          size.i=size.n
  312.          sort.i=sort.n
  313.          file.n=temp
  314.          size.n=stemp
  315.          sort.n=sstemp
  316.          done=0
  317.       end
  318.    end
  319. end
  320.  
  321. /*  Now sort directory names */
  322.  
  323. sort.0=dfile.0
  324. do i=1 to dfile.0
  325.    if POS('C',switch)=0 then PARSE UPPER VAR dfile.i sort.i
  326.    else sort.i=dfile.i
  327. end
  328.  
  329.  
  330. do until done=1
  331.    done=1
  332.    do i=1 to (dfile.0-1)
  333.       n=i+1
  334.       if sort.i>sort.n then do
  335.          temp=dfile.i
  336.          stemp=dsize.i
  337.          sstemp=sort.i
  338.          dfile.i=dfile.n
  339.          dsize.i=dsize.n
  340.          sort.i=sort.n
  341.          dfile.n=temp
  342.          dsize.n=stemp
  343.          sort.n=sstemp
  344.          done=0
  345.       end
  346.    end
  347. end
  348. return
  349.  
  350.  
  351. /*************************HELP FUNCTION*********************************/
  352. help:
  353. say 'D.CMD REXX Directory Display and Sort for OS/2'
  354. say '(c)Copyright 1993'
  355. say''
  356. say 'C.E.Gaumer Software'
  357. say 'P.O. Box 383'
  358. say 'Laurys Station, PA 18059'
  359. say ''
  360. say 'Useage is D [filespec] [/]|[/s /c /n /h /d]'
  361. say 'Filespec is any valid file specification with or without wildcards,'
  362. say ' a drive spec and/or path which may be absolute or relative to'
  363. say ' the current directory'
  364. say ''
  365. say '/  means ignore user set default switches'
  366. say '/s means calculate the size of matching files in all subdirectories'
  367. say '/c means sort case sensitive (lower case after uppercase)'
  368. say '/h means show hidden files (hidden directories always shown)'
  369. say '/n means no pause (good when redirecting output)'
  370. say '/d means show all directories (matching file spec or not)'
  371. say '/? means display this screen'
  372. call SysDropFuncs                                     /*  cleanup         */
  373. EXIT                                                  /* and go home!!    */
  374.