home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / NCD.ZIP / XCD.CMD < prev   
OS/2 REXX Batch file  |  1993-01-28  |  6KB  |  179 lines

  1. /* ================================================================== */
  2. /* REXX XCD - A Norton Change Directory Utility for OS/2              */
  3. /* Copyright (c) 1992 Larry Wasserman.  All Rights Reserved.          */
  4. /* CompuServe  [76666,1043]                                           */
  5. /*                                                                    */
  6. /* Usage : XCD [ [/R] | [partial last node] ]                         */
  7. /* ================================================================== */
  8. SAY 'XCD ver 1.0 Copyright 1993 L. Wasserman'
  9. '@echo off'
  10. PARSE UPPER ARG var1;
  11. var1 = STRIP(var1);
  12.  
  13. /* ================================================================== */
  14. /* Check to see if all REXX utility functions have been Loaded        */
  15. /* ================================================================== */
  16. IF 0 < RxFuncQuery('SysLoadFuncs') THEN 
  17.    DO
  18.       CALL RxFuncAdd 'SysLoadFuncs', 'REXXUTIL', 'SysLoadFuncs'; 
  19.       CALL SysLoadFuncs; 
  20.    END;
  21.  
  22. /* ================================================================== */
  23. /* Check to see if this is just a rebuild                             */
  24. /* ================================================================== */
  25. IF COMPARE(var1, '/R') = 0 THEN
  26.    DO
  27.       CALL REBUILD_TREE '\REXTREE.NCD';
  28.       SIGNAL bailout;
  29.    END
  30.  
  31. /* ================================================================== */
  32. /* If \REXTREE does not exist, rebuild it and open it at the top      */
  33. /* ================================================================== */
  34. myfile = STREAM('\REXTREE.NCD', 'C', 'QUERY EXISTS');
  35. IF LENGTH(myfile) = 0 THEN
  36.    DO
  37.      myfile = '\REXTREE.NCD';
  38.      CALL REBUILD_TREE myfile;
  39.      LINEIN(myfile,1,0);
  40.    END
  41.  
  42. /* ================================================================== */
  43. /* Read the Directory Tree. If no var1 parameter wa supplied then     */
  44. /* enter all directory paths in the dir. stem, otherwise, if the      */
  45. /* <arglen> characters of the last node = the var1 parameter, enter   */
  46. /* the entire path in the dir. stem                                   */
  47. /* ================================================================== */
  48. arglen = LENGTH(var1);
  49. DO WHILE LINES(myfile) > 0
  50.    dirpath = LINEIN(myfile);
  51.    IF arglen = 0 THEN
  52.       DO
  53.          dir.dups = dirpath;
  54.          dups = dups + 1;
  55.       END
  56.    ELSE   
  57.       DO
  58.          len = LENGTH(dirpath);
  59.          x = LASTPOS('\',dirpath);
  60.          str = RIGHT(dirpath, len - x);
  61.          strcmp = LEFT(str,arglen);
  62.          IF COMPARE(var1, strcmp) = 0 THEN 
  63.             DO
  64.                dir.dups = dirpath;
  65.                dups = dups + 1;
  66.             END
  67.       END
  68. END
  69. dups = dups - 1;
  70.  
  71. /* =================================================================== */
  72. /* Show the list of duplicate matches, and find out which one to CD    */
  73. /* =================================================================== */
  74. IF dups = 0 THEN
  75.    DO
  76.       SAY ''
  77.       SAY 'XCD: Directory matching 'var1 'does not exist';
  78.       SIGNAL bailout;
  79.    END
  80. IF dups = 1 THEN
  81.    DO
  82.       answer = 1;
  83.       SIGNAL SWITCH;
  84.    END
  85. fini = 0;
  86. DO UNTIL fini = 1
  87.    SAY '';
  88.    DO i = 1 to dups
  89.       SAY '   ' i'. ' dir.i;
  90.    END
  91.    SAY '';
  92.    x = CHAROUT(, '    To which directory do you want to switch? ');
  93.    answer = SysGetKey();
  94.    SAY ''
  95.    IF C2X(answer) = 1B THEN
  96.       SIGNAL bailout;
  97.    IF ((DATATYPE(answer, 'W') = 0) | (answer < 1) | (answer > dups)) THEN
  98.       DO
  99.         SAY '';
  100.         SAY '' ;
  101.         SAY 'Invalid input...Enter a number from 1 to 'dups;
  102.       END
  103.    ELSE
  104.       fini = 1
  105. END
  106.  
  107. /* ================================================================== */
  108. /* CD to the new directory, and exit                                  */
  109. /* ================================================================== */
  110. SWITCH:;
  111. /* SAY ''; */
  112. /* SAY 'Changing directory to 'dir.answer; */
  113. '@CD' dir.answer;
  114.  
  115. BAILOUT:;
  116. EXIT;
  117.  
  118. REBUILD_TREE:PROCEDURE
  119. ARG myfile
  120.  
  121. SAY '';
  122. rc = CHAROUT(STDOUT, '   Building Directory Tree ');
  123.  
  124. /* =================================================================== */
  125. /* Get the list of directories on the current disk                     */
  126. /* =================================================================== */
  127. IF SysFileTree('\*.*', 'dirlst', 'DSO') > 0 THEN
  128.    DO
  129.       SAY '';
  130.       SAY 'Not enough memory to read the directory tree...terminating';
  131.       SIGNAL bailout;
  132.    END
  133.  
  134. /* ================================================================== */
  135. /* Sort the list of directories                                       */
  136. /* ================================================================== */
  137. iLo = 1; iHi = dirlst.0;
  138. DO FOREVER;
  139.    IF iLo > iHi THEN LEAVE;
  140.    i = iLo;
  141.    cLo = SUBSTR(dirlst.iLo,3,LENGTH(dirlst.iLo)-2)
  142.    cHi = SUBSTR(dirlst.iHi,3,LENGTH(dirlst.iHi)-2)
  143.    DO FOREVER;
  144.       IF i > iHi THEN LEAVE;
  145.       cX = SUBSTR(dirlst.i,3,LENGTH(dirlst.i)-2)
  146.       iSwap = 0;
  147.       IF cX < cLo THEN DO;
  148.          iSwap = 1;
  149.          cExch = dirlst.i; dirlst.i = dirlst.iLo; dirlst.iLo = cExch;
  150.          cLo = SUBSTR(dirlst.iLo,3,LENGTH(dirlst.iLo)-2)
  151.          cX  = SUBSTR(dirlst.i,3,LENGTH(dirlst.i)-2)
  152.       end;
  153.       IF cX > cHi THEN do;
  154.          iSwap = 1;
  155.          cExch = dirlst.i; dirlst.i = dirlst.iHi; dirlst.iHi = cExch;
  156.          cHi = SUBSTR(dirlst.iHi,3,LENGTH(dirlst.iHi)-2)
  157.          cX  = SUBSTR(dirlst.i,3,LENGTH(dirlst.i)-2)
  158.       end;
  159.       IF iSwap < 1 THEN i = i+1;
  160.    end;
  161.    iLo = iLo+1; iHi = iHi-1;
  162.    rc = CHAROUT(STDOUT, '.');
  163. end;
  164.  
  165. /* =================================================================== */
  166. /* Open (or create) the REXTREE.NCD file and write it                  */
  167. /* =================================================================== */
  168. CALL LINEOUT myfile, dirlst.1, 1;  /* opens file and overwrites first line */
  169. DO i = 2 to dirlst.0
  170.    CALL LINEOUT myfile, dirlst.i;
  171.    IF RESULT = 1 THEN
  172.       DO
  173.          SAY 'Unable to write treefile...terminating';
  174.          SIGNAL bailout;
  175.       END
  176. END
  177. rc = LINEOUT(myfile);    /* closes file */
  178. RETURN;
  179.