home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 18 REXX
/
18-REXX.zip
/
rcd14.zip
/
RCD.CMD
next >
Wrap
OS/2 REXX Batch file
|
1993-09-01
|
10KB
|
233 lines
/*===========================================================================*
* RCD.CMD version 1.4 *
* *
* This REXX script can be used to quickly change the current working *
* directory, by searching the directory structure for directories that *
* partially match the user supplied argument. This is my first try at *
* creating a useful REXX script. This script is FREEWARE. Please use it *
* all you wish, and if you can improve it, please do so. All I ask is *
* that you drop me a note with a copy of the improvements. You can *
* contact me with suggestions or bug reports via email on the Internet at *
* 'robber@netcom.com' or CIS 70451,2661. Thanks. *
* *
*---------------------------------------------------------------------------*
* Usage: rcd [-r| r] [-s|/s] <dirname> *
* *
* Flags: -r Reads current directory structure and updates the rcd *
* database. The database is kept in the hidden file *
* \RCD.DB on the current drive and is automatically *
* created, if it does not exist. *
* *
* -s Matches <dirname> with any substring in the target, *
* instead of the prefix of the target directory. *
* *
* Changes current directory to a directory named in the database that *
* matches or has <dirname> as a prefix. If there is more than one *
* possible destination then each choice is shown and the ENTER key *
* chooses the destination. The TAB and arrow keys move through the *
* list and the ESC key quits without changing the directory. *
*---------------------------------------------------------------------------*
* Developed by: Robert D. Reynolds RDR robber@netcom.com *
* Suggestions by: V. Phaniraj VP phaniraj@plains.nodak.edu *
* Henry So, Jr. HTS henryso@tasha.poly.edu *
* *
* The beginning: 07/07/92 RDR *
* v1.0: 07/10/92 RDR *
* v1.1: 07/11/92 RDR - Fixed bug with spaces in directory *
* names. *
* v1.2: 07/14/92 RDR - Uses more REXX internal functions *
* fixes bug when target was not found *
* and problem with $DIRCMD. *
* v1.3: 08/31/93 RDR - Fixed some bugs and added -S flag *
* VP - add immediate change to subdirectory *
* v1.4: 09/01/93 RDR - Made database a hidden file *
* HTS - change so database retains case *
* *
*===========================================================================*/
"@ECHO OFF"
/*-- Get arguments and load external functions ------------------------------*/
ARG target rest
CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysloadFuncs'
CALL SysLoadFuncs
/*-- Set up variables -------------------------------------------------------*/
version = "1.4"
data_file = '\RCD.DB'
re_read_db = 0
sub_strings = 0
CALL SysFileTree data_file, 'db.', 'FO', '*****'
exist_db = db.0
/*-- Check for flags and trim target ----------------------------------------*/
DO UNTIL rest=""
IF target="-R" | target="/R"
THEN DO
re_read_db = 1
PARSE VAR rest target rest
END
IF target="-S" | target="/S"
THEN DO
sub_strings = 1
PARSE VAR rest target rest
END
END
IF target<>"\" & target<>"/" THEN target = FileSpec('NAME',target)
/*-- Create database if necessary -------------------------------------------*/
IF exist_db=0 | re_read_db=1
THEN DO
IF exist_db=1
THEN DO
IF SysFileDelete( data_file )<>0
THEN DO
SAY " Unable to delete database; Aborting!"
EXIT
END
END
SAY " Creating the database for this drive...."
CALL SysFileTree '\*', 'db.', 'DSO'
DO i = 1 TO db.0
CALL LineOut data_file, db.i
if RESULT=1 THEN LEAVE
END
junk = Stream(data_file,C,'CLOSE')
IF RESULT=0 & SysFileTree( data_file, 'db.', 'F', '*****', '**+**')=0
THEN
SAY " Database created successfully!"
ELSE DO
SAY " Error occurred while creating the database!"
EXIT
END
IF target="" THEN EXIT
END
/*-- IF no parameters display usage message ---------------------------------*/
IF target="" THEN CALL Print_Usage_Info
/*-- Check for special subdirectory cases -----------------------------------*/
IF target="\" | target="/" THEN CALL Change_Dir "\" /* Test for root */
IF DIRECTORY( target )<>"" THEN EXIT /* Test for local subdirectory */
/* Thanks to VP for this idea */
/*-- Scan database ----------------------------------------------------------*/
dir_list.0 = 0
matches.0 = 0
possible_dirs.0 = 0
current = TRANSLATE( SUBSTR( DIRECTORY(), 3))
CALL SysFileSearch target, data_file, 'dir_list.'
DO i = 1 TO dir_list.0
dir=TRANSLATE( dir_list.i )
IF dir=current THEN ITERATE /* Don't include if current dir */
subd = TRANSLATE( FILESPEC( "NAME", dir ))
IF POS(target,subd)=1 | ( sub_strings=1 & POS(target,subd)>1 )
THEN DO /* If prefix add to possible_dirs */
IF target=subd /* If exact match add to matches */
THEN DO
j = matches.0 + 1
matches.j = dir_list.i
matches.0 = j
END
j = possible_dirs.0 + 1
possible_dirs.j = dir_list.i
possible_dirs.0 = j
END
END
/*-- Select directory -------------------------------------------------------*/
SELECT
WHEN matches.0=1 /* Just one match */
THEN CALL Change_Dir matches.1
WHEN possible_dirs.0=1 /* Just one possible */
THEN CALL Change_Dir possible_dirs.1
WHEN possible_dirs.0>1 /* Ask user what directory */
THEN DO
found = Find_Match()
IF found<>0 THEN CALL Change_Dir possible_dirs.found
END
OTHERWISE
SAY " No Match Found!"
END
EXIT
/*-- Print usage information ------------------------------------------------*/
/* This procedure displays RCD's usage information to the user */
/*---------------------------------------------------------------------------*/
Print_Usage_Info:
SAY " RCD v" || version || " by Robert D. Reynolds"
SAY ""
SAY " Usage: rcd [-r|/r] [-s|/s] <dirname>"
SAY ""
SAY " Flags: -r Reads current directory structure and updates the rcd"
SAY " database. The database is kept in the hidden file"
SAY " \RCD.DB on the current drive and is automatically"
SAY " created, if it does not exist."
SAY ""
SAY " -s Matches <dirname> with any substring in the target,"
SAY " instead of the prefix of the target directory."
SAY ""
SAY " Changes current directory to a directory named in the database that"
SAY " matches or has <dirname> as a prefix. If there is more than one"
SAY " possible destination then each choice is shown and the <ENTER> key"
SAY " chooses the destination. The <TAB> and arrow keys move through the"
SAY " list and the <ESC> key quits without changing the directory."
EXIT
/*-- Find directory that matches --------------------------------------------*/
/* This procedure displays each possible directory choice to the user and */
/* the user interactively chooses the correct directory to change to */
/*---------------------------------------------------------------------------*/
Find_Match:
i = 1
DO FOREVER
SAY " CD to " || Left(Strip(Right(possible_dirs.i,72)),72)
PARSE VALUE SysCurPos() WITH row col
inkey = 0
DO WHILE inkey=224 | inkey=0 /* Throw away modifier key */
PARSE VALUE SysGetKey('NOECHO') WITH key
inkey = c2d(key)
END
SELECT
WHEN inkey=27 /* ESC */
THEN RETURN 0
WHEN inkey=72 | inkey=75 | inkey=15 /* UP,RT,SHFT-TAB */
THEN IF i<>1 THEN i = i - 1
WHEN inkey=13 /* CR */
THEN RETURN i
OTHERWISE
IF i<>possible_dirs.0 THEN i = i + 1
END
junk = SysCurPos(row-1,0)
END
/*-- Change current directory -----------------------------------------------*/
/* This procedure changes the current directory for the user */
/*---------------------------------------------------------------------------*/
Change_Dir:
ARG d
CALL DIRECTORY d
EXIT
/*---------------------------------------------------------------------------*/