home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / RCD2.ZIP / RCD2.CMD
OS/2 REXX Batch file  |  1992-10-07  |  10KB  |  232 lines

  1. /*===========================================================================* 
  2.  *  RCD.CMD                                                     version 1.3  *
  3.  *                                                                           * 
  4.  *  This REXX script can be used like the NCD command that is part of Norton * 
  5.  *  Utilities.  This is my first try at creating a useful REXX script.       * 
  6.  *  This script is FREEWARE.  Please use it all you wish, and If you can     * 
  7.  *  improve it, please do so.  All I ask is that if you change this command  * 
  8.  *  and distribute an improved version, that I get a copy of the changes.    * 
  9.  *  You can contact me with suggestions or bug reports on the Internet at    * 
  10.  *  'easyrob@cs.utexas.edu'.  Thanks.                                        * 
  11.  *                                                                           * 
  12.  *---------------------------------------------------------------------------* 
  13.  *  Usage: rcd [-r| r] <dirname>                                             * 
  14.  *                                                                           * 
  15.  *  Flags: -r      Reads current directory structure and updates the         * 
  16.  *                 rcd database.  The database is kept in \rcd.db on         * 
  17.  *                 the current drive and is automatically created If         * 
  18.  *                 it does not exist.                                        * 
  19.  *                                                                           * 
  20.  *  Changes current directory to a directory named in the database that      * 
  21.  *  matches or has <dirname> as a substring.  If there is more than one      * 
  22.  *  possible destination then each choice is shown and the ENTER key         * 
  23.  *  chooses the destination.  The TAB and arrow keys move through the        * 
  24.  *  list and the ESC key quits without changing the directory.               * 
  25.  *---------------------------------------------------------------------------* 
  26.  *  Developed by:  Robert D. Reynolds                                        * 
  27.  *  1.3 Modifications by:  Byron Stephan                                     *
  28.  *                         Email through OS/2 Shareware BBS                  *                                                  * 
  29.  *  The beginning: 07/07/92       RDR                                        * 
  30.  *           v1.0: 07/10/92       RDR                                        * 
  31.  *           v1.1: 07/11/92       RDR - Fixed bug with spaces in directory   * 
  32.  *                                      names.                               * 
  33.  *           v1.2: 07/14/92       RDR - Uses more REXX internal functions    * 
  34.  *                                      fixes bug when target was not found  * 
  35.  *                                      and problem with $DIRCMD.            * 
  36.  *           v1.3: 09/13/92       BPS - Uses SysFileTree more effciently.    *
  37.  *                                      Uses correct database when target is *
  38.  *                                      on a differnt drive.                 *
  39.  *                                      Database for LAN drives is created   *
  40.  *                                      on drive C:                          *
  41.  *                                                                           *
  42.  *===========================================================================*/
  43.    /* check whether RxFuncs are loaded, if not, load them */
  44.    IF RxFuncQuery('SysLoadFuncs') THEN
  45.    DO
  46.       /* load the load-function */
  47.       CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'       
  48.  
  49.       /* load the Sys* utilities */
  50.       CALL SysLoadFuncs                                                 
  51.    END
  52. "@ECHO OFF"
  53.  
  54. /*-- Get arguements ---------------------------------------------------------*/
  55.  
  56. Arg arguements
  57. Parse Var arguements target rest
  58.  
  59. /*-- Set up variables -------------------------------------------------------*/
  60.  
  61. version = "1.3"
  62. re_read_db = 0
  63. DriveMap=SysDriveMap(,'Local')
  64.  
  65.  
  66. /*-- Check for re-read database flag and trim Target ------------------------*/
  67.  
  68. If target="-R" | target="/R" Then Do
  69.    re_read_db = 1
  70.    If rest<>"\" & rest<>"/" Then do
  71.       TargetDrive = FileSpec('Drive',rest)
  72.       target = FileSpec('NAME',rest)
  73.    end  /* Do */
  74. End
  75. Else
  76.    If target<>"\" & target<>"/" Then do
  77.       TargetDrive = FileSpec('Drive',target)
  78.       target = FileSpec('NAME',target)
  79.    end  /* Do */
  80.  
  81. /*-- Set database to Find database or set to local ------------------------------------------*/
  82.  
  83. If TargetDrive='' then
  84.    DatabaseDrive=FileSpec('Drive',directory())
  85. else
  86.    DatabaseDrive=TargetDrive
  87.  
  88. if pos(DatabaseDrive,DriveMap) > 0 then
  89.    data_base = DatabaseDrive||'\rcd.db'
  90. else
  91.    data_base = 'c:\rcd_'||Substr(DatabaseDrive,1,1)||'.db'
  92.  
  93. exist_db = Lines(data_base)
  94. junk = Stream(data_base,C,'CLOSE')
  95.  
  96. /*-- Create database If necessary -------------------------------------------*/
  97. If exist_db=0 | re_read_db=1 Then
  98. Do
  99.      If re_read_db=1 & exist_db=1 Then 
  100.      Do
  101.          Call RxFuncAdd 'SysFileDelete', 'RexxUtil', 'SysFileDelete'
  102.          rc = SysFileDelete(data_base)
  103.          If rc<>0 Then
  104.          Do
  105.              Say " Unable to delete database; Aborting!"
  106.              Exit
  107.          End
  108.      End
  109.      Say " Creating the database for this drive...."
  110.      Call RxFuncAdd 'SysFileTree', 'RexxUtil', 'SysFileTree'
  111.      Call SysFileTree '\*', 'db.', 'SDO'
  112.      Do i = 1 to db.0
  113.          Call LineOut data_base, db.i
  114.          if RC=1 Then Leave
  115.      End
  116.      junk = Stream(data_base,C,'CLOSE')
  117.      If RC=0 Then
  118.          Say " Database created successfully!"
  119.      Else
  120.      Do
  121.          Say " Error occurred while creating the database!"
  122.          Exit
  123.      End
  124. End
  125.  
  126. /*-- If no parameters display usage message ---------------------------------*/
  127.  
  128. If target="" & re_read_db=0 Then
  129. Do
  130.     Say " RCD v" || version || " by Robert D. Reynolds"
  131.     Say ""
  132.     Say " Usage: rcd [-r|/r] DirName"
  133.     Say ""
  134.     Say " Flags: -r     Reads current directory structure and updates the"
  135.     Say "               rcd database.  The database is kept in \rcd.db on"
  136.     Say "               the current drive and is automatically created If"
  137.     Say "               it does not exist."
  138.     Say ""
  139.     Say " Changes current directory to a directory named in the database that"
  140.     Say " matches or has DirName as a substring.  If there is more than one"
  141.     Say " possible destination then each choice is shown and the <ENTER> key"
  142.     Say " chooses the destination.  The <TAB> and arrow keys move through the"
  143.     Say " list and the <ESC> key quits without changing the directory."
  144.  
  145.     Exit
  146. End
  147.  
  148. If target="" Then Exit 
  149. If target="\" | target="/" Then 
  150. Do
  151.     Call Directory "\"
  152.     Exit
  153. End
  154.  
  155. /*-- Scan database ----------------------------------------------------------*/
  156.  
  157. dir_list.0 = 0
  158. matches.0 = 0
  159. possible_dirs.0 = 0
  160. current = directory()
  161.  
  162. Call RxFuncAdd 'SysFileSearch', 'RexxUtil', 'SysFileSearch'
  163. Call SysFileSearch target, data_base, 'dir_list.'
  164.  
  165. Do i = 1 to dir_list.0
  166.     d = Translate( dir_list.i )
  167.     subd = FileSpec("NAME",d)
  168.     If d=current Then                 /* Do not include If current directory */
  169.         Iterate
  170.     If target=subd Then               /* If exact match add to matches */
  171.     Do
  172.         j = matches.0 + 1
  173.         matches.j = d
  174.         matches.0 = j
  175.     End
  176.     If Pos(target,subd)>=1 Then       /* If substring add to possible_dirs */
  177.     Do
  178.         j = possible_dirs.0 + 1
  179.         possible_dirs.j = d
  180.         possible_dirs.0 = j
  181.     End
  182. End
  183.  
  184. /*-- Change directory -------------------------------------------------------*/
  185.  
  186. Select
  187.     When matches.0=1                              /* Just one match */
  188.         Then Call Directory matches.1
  189.     When possible_dirs.0=1                        /* Just one possible */
  190.         Then Call Directory possible_dirs.1
  191.     When possible_dirs.0>1                        /* Ask user what directory */
  192.         Then
  193.         Do
  194.             Call RxFuncAdd 'SysCurPos', 'RexxUtil', 'SysCurPos'
  195.             Call RxFuncAdd 'SysGetKey', 'RexxUtil', 'SysGetKey'
  196.  
  197.             found=0
  198.             i = 1 
  199.             Do Forever
  200.                 Say Left(Strip(Right(possible_dirs.i,79)),79)
  201.                 Parse Value SysCurPos() With row col
  202.                 inkey = 0
  203.                 Do While inkey=224 | inkey=0      /* Throw away modIfier key */
  204.                     Parse Value SysGetKey('NOECHO') With key
  205.                     inkey = c2d(key)
  206.                 End
  207.                 select
  208.                     When inkey=27                          /*ESC*/
  209.                         Then Exit 
  210.                     When inkey=72 | inkey=75 | inkey=15    /* UP,RT,SHFT-TAB */
  211.                         Then If I<>1 Then I = I - 1
  212.                     When inkey=13                          /* CR */
  213.                         Then
  214.                         Do 
  215.                             found=1
  216.                             Leave
  217.                         End
  218.                 Otherwise
  219.                     If I<>possible_dirs.0 Then I = I + 1
  220.                 End
  221.                 junk = SysCurPos(row-1,0)
  222.             End
  223.             If found Then Call Directory possible_dirs.i
  224.         End
  225. Otherwise
  226.     Say " No Match Found!"
  227. End
  228.  
  229. Exit
  230.  
  231. /*---------------------------------------------------------------------------*/
  232.