home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rcd14.zip / RCD.CMD next >
OS/2 REXX Batch file  |  1993-09-01  |  10KB  |  233 lines

  1. /*===========================================================================* 
  2.  *  RCD.CMD                                                     version 1.4  * 
  3.  *                                                                           * 
  4.  *  This REXX script can be used to quickly change the current working       * 
  5.  *  directory, by searching the directory structure for directories that     * 
  6.  *  partially match the user supplied argument.  This is my first try at     * 
  7.  *  creating a useful REXX script.  This script is FREEWARE.  Please use it  * 
  8.  *  all you wish, and if you can improve it, please do so.  All I ask is     * 
  9.  *  that you drop me a note with a copy of the improvements.  You can        * 
  10.  *  contact me with suggestions or bug reports via email on the Internet at  * 
  11.  *  'robber@netcom.com' or CIS 70451,2661.  Thanks.                          * 
  12.  *                                                                           * 
  13.  *---------------------------------------------------------------------------* 
  14.  *  Usage: rcd [-r| r] [-s|/s] <dirname>                                     * 
  15.  *                                                                           * 
  16.  *  Flags: -r      Reads current directory structure and updates the rcd     * 
  17.  *                 database.  The database is kept in the hidden file        * 
  18.  *                 \RCD.DB on the current drive and is automatically         * 
  19.  *                 created, if it does not exist.                            * 
  20.  *                                                                           * 
  21.  *         -s     Matches <dirname> with any substring in the target,        * 
  22.  *                instead of the prefix of the target directory.             * 
  23.  *                                                                           * 
  24.  *  Changes current directory to a directory named in the database that      * 
  25.  *  matches or has <dirname> as a prefix.  If there is more than one         * 
  26.  *  possible destination then each choice is shown and the ENTER key         * 
  27.  *  chooses the destination.  The TAB and arrow keys move through the        * 
  28.  *  list and the ESC key quits without changing the directory.               * 
  29.  *---------------------------------------------------------------------------* 
  30.  *  Developed by:    Robert D. Reynolds      RDR   robber@netcom.com         * 
  31.  *  Suggestions by:  V. Phaniraj             VP    phaniraj@plains.nodak.edu * 
  32.  *                   Henry So, Jr.           HTS   henryso@tasha.poly.edu    * 
  33.  *                                                                           * 
  34.  *  The beginning: 07/07/92       RDR                                        * 
  35.  *           v1.0: 07/10/92       RDR                                        * 
  36.  *           v1.1: 07/11/92       RDR - Fixed bug with spaces in directory   * 
  37.  *                                      names.                               * 
  38.  *           v1.2: 07/14/92       RDR - Uses more REXX internal functions    * 
  39.  *                                      fixes bug when target was not found  * 
  40.  *                                      and problem with $DIRCMD.            * 
  41.  *           v1.3: 08/31/93       RDR - Fixed some bugs and added -S flag    * 
  42.  *                                VP  - add immediate change to subdirectory * 
  43.  *           v1.4: 09/01/93       RDR - Made database a hidden file          * 
  44.  *                                HTS - change so database retains case      * 
  45.  *                                                                           * 
  46.  *===========================================================================*/
  47. "@ECHO OFF"
  48.  
  49. /*-- Get arguments and load external functions ------------------------------*/
  50.  
  51. ARG target rest
  52.  
  53. CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysloadFuncs'
  54. CALL SysLoadFuncs
  55.  
  56. /*-- Set up variables -------------------------------------------------------*/
  57.  
  58. version = "1.4"
  59. data_file = '\RCD.DB'
  60. re_read_db = 0
  61. sub_strings = 0
  62.  
  63. CALL SysFileTree data_file, 'db.', 'FO', '*****'
  64. exist_db = db.0
  65.  
  66. /*-- Check for flags and trim target ----------------------------------------*/
  67.  
  68. DO UNTIL rest=""
  69.     IF target="-R" | target="/R" 
  70.     THEN DO
  71.         re_read_db = 1
  72.         PARSE VAR rest target rest
  73.     END
  74.     IF target="-S" | target="/S"
  75.     THEN DO
  76.         sub_strings = 1
  77.         PARSE VAR rest target rest
  78.     END
  79. END
  80.  
  81. IF target<>"\" & target<>"/" THEN target = FileSpec('NAME',target)
  82.  
  83. /*-- Create database if necessary -------------------------------------------*/
  84.  
  85. IF exist_db=0 | re_read_db=1 
  86. THEN DO
  87.     IF exist_db=1 
  88.     THEN DO
  89.         IF SysFileDelete( data_file )<>0
  90.         THEN DO
  91.             SAY " Unable to delete database; Aborting!"
  92.             EXIT
  93.         END
  94.     END
  95.     SAY " Creating the database for this drive...."
  96.     CALL SysFileTree '\*', 'db.', 'DSO'
  97.     DO i = 1 TO db.0
  98.         CALL LineOut data_file, db.i
  99.         if RESULT=1 THEN LEAVE
  100.     END
  101.     junk = Stream(data_file,C,'CLOSE')
  102.     IF RESULT=0 & SysFileTree( data_file, 'db.', 'F', '*****', '**+**')=0
  103.     THEN
  104.         SAY " Database created successfully!"
  105.     ELSE DO
  106.         SAY " Error occurred while creating the database!"
  107.         EXIT
  108.     END
  109.     IF target="" THEN EXIT
  110. END
  111.  
  112. /*-- IF no parameters display usage message ---------------------------------*/
  113.  
  114. IF target="" THEN CALL Print_Usage_Info
  115.  
  116. /*-- Check for special subdirectory cases -----------------------------------*/
  117.  
  118. IF target="\" | target="/" THEN CALL Change_Dir "\"         /* Test for root */
  119.  
  120. IF DIRECTORY( target )<>"" THEN EXIT          /* Test for local subdirectory */
  121.                                               /* Thanks to VP for this idea  */
  122.  
  123. /*-- Scan database ----------------------------------------------------------*/
  124.  
  125. dir_list.0 = 0
  126. matches.0 = 0
  127. possible_dirs.0 = 0
  128. current = TRANSLATE( SUBSTR( DIRECTORY(), 3))
  129.  
  130. CALL SysFileSearch target, data_file, 'dir_list.'
  131.  
  132. DO i = 1 TO dir_list.0
  133.  
  134.     dir=TRANSLATE( dir_list.i )
  135.     IF dir=current THEN ITERATE       /* Don't include if current dir */
  136.  
  137.     subd = TRANSLATE( FILESPEC( "NAME", dir ))
  138.     IF POS(target,subd)=1 | ( sub_strings=1 & POS(target,subd)>1 ) 
  139.     THEN DO                                  /* If prefix add to possible_dirs */
  140.         IF target=subd                      /* If exact match add to matches */
  141.         THEN DO
  142.             j = matches.0 + 1
  143.             matches.j = dir_list.i
  144.             matches.0 = j
  145.         END
  146.         j = possible_dirs.0 + 1
  147.         possible_dirs.j = dir_list.i
  148.         possible_dirs.0 = j
  149.     END
  150. END
  151.  
  152. /*-- Select directory -------------------------------------------------------*/
  153.  
  154. SELECT
  155.     WHEN matches.0=1                              /* Just one match */
  156.         THEN CALL Change_Dir matches.1
  157.     WHEN possible_dirs.0=1                        /* Just one possible */
  158.         THEN CALL Change_Dir possible_dirs.1
  159.     WHEN possible_dirs.0>1                        /* Ask user what directory */
  160.         THEN DO
  161.             found = Find_Match()
  162.             IF found<>0 THEN CALL Change_Dir possible_dirs.found
  163.         END
  164. OTHERWISE
  165.     SAY " No Match Found!"
  166. END
  167.  
  168. EXIT
  169.  
  170. /*-- Print usage information ------------------------------------------------*/
  171. /*   This procedure displays RCD's usage information to the user             */
  172. /*---------------------------------------------------------------------------*/
  173. Print_Usage_Info:
  174.  
  175.     SAY " RCD v" || version || " by Robert D. Reynolds"
  176.     SAY ""
  177.     SAY " Usage: rcd [-r|/r] [-s|/s] <dirname>"
  178.     SAY ""
  179.     SAY " Flags: -r     Reads current directory structure and updates the rcd"
  180.     SAY "               database.  The database is kept in the hidden file"
  181.     SAY "               \RCD.DB on the current drive and is automatically"
  182.     SAY "               created, if it does not exist."
  183.     SAY ""
  184.     SAY "        -s     Matches <dirname> with any substring in the target,"
  185.     SAY "               instead of the prefix of the target directory."
  186.     SAY ""
  187.     SAY " Changes current directory to a directory named in the database that"
  188.     SAY " matches or has <dirname> as a prefix.  If there is more than one"
  189.     SAY " possible destination then each choice is shown and the <ENTER> key"
  190.     SAY " chooses the destination.  The <TAB> and arrow keys move through the"
  191.     SAY " list and the <ESC> key quits without changing the directory."
  192.  
  193.     EXIT
  194.  
  195. /*-- Find directory that matches --------------------------------------------*/
  196. /*   This procedure displays each possible directory choice to the user and  */
  197. /*   the user interactively chooses the correct directory to change to       */
  198. /*---------------------------------------------------------------------------*/
  199. Find_Match:
  200.  
  201.     i = 1 
  202.     DO FOREVER
  203.         SAY " CD to " || Left(Strip(Right(possible_dirs.i,72)),72)
  204.         PARSE VALUE SysCurPos() WITH row col
  205.         inkey = 0
  206.         DO WHILE inkey=224 | inkey=0      /* Throw away modifier key */
  207.             PARSE VALUE SysGetKey('NOECHO') WITH key
  208.             inkey = c2d(key)
  209.         END
  210.         SELECT
  211.             WHEN inkey=27                          /* ESC */
  212.                 THEN RETURN 0 
  213.             WHEN inkey=72 | inkey=75 | inkey=15    /* UP,RT,SHFT-TAB */
  214.                 THEN IF i<>1 THEN i = i - 1
  215.             WHEN inkey=13                          /* CR */
  216.                 THEN RETURN i
  217.         OTHERWISE
  218.             IF i<>possible_dirs.0 THEN i = i + 1
  219.         END
  220.         junk = SysCurPos(row-1,0)
  221.     END
  222.  
  223. /*-- Change current directory -----------------------------------------------*/
  224. /*   This procedure changes the current directory for the user               */
  225. /*---------------------------------------------------------------------------*/
  226. Change_Dir:
  227.  
  228.     ARG d
  229.     CALL DIRECTORY d
  230.     EXIT
  231.  
  232. /*---------------------------------------------------------------------------*/
  233.