home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
r
/
retain11.zip
/
RETAIN.BAT
< prev
next >
Wrap
DOS Batch File
|
1992-12-10
|
14KB
|
311 lines
echo off
rem ===RETAIN.BAT, vs. 1.1, 10/12-92===
if "%1"=="" echo --- RETAIN 1.1 (c) 1992, Jim Groeneveld; retains current path up to 8 levels.
if "%1"=="?" goto help
if "%1"=="/?" goto help
if "%1"=="-?" goto help
if "%1"==".path" goto path
if "%1"==".notfound" goto nopath
if "%1"=="*" if exist C:\RETURN.BAT C:\RETURN *
rem line above for DOS 2.x should be changed as below
rem 'if "%1"=="*" if exist C:\RETURN.BAT RETURN *'
if "%1"=="*" if not exist C:\RETURN.BAT goto nolist
if "%1"==".list" goto list
if "%1"==".recreate" goto create
if "%1"==".return" goto RETURN
if "%1"==".reversed" goto RETURN
if not "%1"=="" goto RETAIN
rem %1 is the location or specification of the auxiliary file $RETURN$.AUX
:determine
rem Determination of location (and name) of auxiliary file $RETURN$.AUX in/as:
rem a. the first and only parameter when calling RETAIN (not checked here)
rem 1. the full _path_ only, incl. trailing '\' (e.g. 'RETAIN C:\UTIL\') or
rem 2. the full _file_specification_ (e.g. 'RETAIN C:\UTIL\AUX-FILE.$$$')
rem b. the environment variable %RETAIN%
rem 1. the full _path_ only, incl. trailing '\' ('SET RETAIN=C:\UTIL\') or
rem 2. the full _file_specification_ ('SET RETAIN=C:\UTIL\AUX-FILE.$$$')
rem c. the ROOT directory of C: (C:\) (or automatically any APPENDed path) or
rem d. the ROOT directory of the CURRENT drive (\) or
rem e. the CURRENT directory (.\) or
rem f. the PARENT directory of the current one (..\) or
rem g. the SAME directory where RETAIN.BAT is residing:
rem if EXPLICITELY specified when starting RETAIN (DOS 3.x&up only).
rem (NOT if RETAIN.BAT can only be found via the PATH!) or
rem h. the DOS PATH variable (this may take quite a while)
rem If the environment variable has not been set: try other location(s)
if "%RETAIN%"=="" goto no_env
rem If $RETURN$.AUX found as file specified by %RETAIN% then restart this batch
rem file (RETAIN.BAT as %0) with _full_path_&_file_name_ as the first parameter
if exist %RETAIN% %0 %RETAIN%
rem If $RETURN$.AUX found in path specified by %RETAIN% then restart this batch
rem file (RETAIN.BAT as %0) with _full_path_&_file_name_ as the first parameter
if exist %RETAIN%$RETURN$.AUX %0 %RETAIN%$RETURN$.AUX
rem If the auxiliary file thus can not be found: try other location(s) below:
:no_env
rem Check the presence of $RETURN$.AUX in (and specify _full_path_&_file_name_
rem if found):
rem (the root of C:)
if exist C:\$RETURN$.AUX %0 C:\$RETURN$.AUX
rem (the root of the current drive)
if exist \$RETURN$.AUX %0 \$RETURN$.AUX
rem (the current directory)
if exist .\$RETURN$.AUX %0 .\$RETURN$.AUX
rem (the parent directory)
if exist ..\$RETURN$.AUX %0 ..\$RETURN$.AUX
rem (the explicitely specified directory)
if exist %0\..\$RETURN$.AUX %0 %0\..\$RETURN$.AUX
rem ---DOSPATH---
rem Within the subroutine :path to be called now by subsequent 'shift'ing
rem the call name for RETAIN (%0) gets lost. To solve this
rem problem a temporary batch file $RETAIN$.BAT is created initially (in
rem the current directory) that contains this information hardcoded and
rem calls RETAIN (or whatever it is renamed) with the necessary parameter:
echo echo off>$RETAIN$.BAT
rem If no write of this auxiliary file is possible then cancel finally:
if not exist $RETAIN$.BAT goto nopath
echo %0 %%1>>$RETAIN$.BAT
rem At the end (or somewhere suited) remove the temporary file $RETAIN$.BAT .
rem Depending on the (path and) name (length) for RETAIN the path environment
rem variable will be used up to a certain number of characters, maximally
rem around 100.
%0 .path %path%
:nopath
echo *** Necessary auxilary file $RETURN$.AUX could not be found - abort!
echo.
echo Specify environment variable RETAIN to point to the path where
echo $RETURN$.AUX resides (either from the DOS prompt or in AUTOEXEC.BAT).
echo E.g. SET RETAIN=C:\UTIL\ (terminated with backslash).
echo Or specify full path and file name to the eventual alternate auxiliary
echo file for $RETURN$.AUX (e.g. SET RETAIN=C:\UTIL\AUX-FILE.$$$).
echo.
echo Or specify path (terminated with '\') to or full file specification
echo for $RETURN$.AUX as the first (and only) parameter for %0
goto end
:RETAIN
if not "%2"=="" goto auxpath_checked
rem Now %2 is empty: aux-path[aux-file] %1 specified by user, check existence:
rem If %1 is _full_path_&_file_name_ of (alternate) $RETURN$.AUX then continue
if exist %1 goto auxpath_checked
rem If %1 is path only to explicit $RETURN$.AUX then restart RETAIN (%0) with
rem _full_path_&_file_name_ as the first parameter
if exist %1$RETURN$.AUX %0 %1$RETURN$.AUX
rem %1 has been specified by the user, but $RETURN$.AUX could not be found:
echo *** Auxiliary path[file] %1 is incorrect, file $RETURN$.AUX not found.
echo Now trying to find $RETURN$.AUX using path (or full file specification):
echo 1. from environment variable RETAIN (path or full file specification)
echo 2. the root directory of C: (C:\) (or automatically any APPENDed path)
echo 3. the root of the current drive (\)
echo 4. the current directory (.\)
echo 5. the parent directory (..\)
echo 6. the same directory as RETAIN.BAT if RETAIN has been called by path
echo 7. from the DOS PATH variable (this may take quite a while)
echo.
goto determine
:auxpath_checked
rem Check for existence of C:\RETURN.BAT: if so, nesting has to take place.
rem This is done by adding the current path parameter after the already
rem present path parameters in C:\RETURN.BAT, where it calls RETAIN.
if not exist C:\RETURN.BAT goto create
rem %2 (and succeeding parameters) is the retained drive and subdirectory
rem If path(s) stored are known resume with recreation of C:\RETURN.BAT
if not "%2"=="" goto create
rem If no stored paths are known (just started RETAIN) force RETURN to
rem supply the already stored path(s) (to add the current path to).
rem This returns all currently stored paths as additional parameters up to 8
C:\RETURN %0 %1 .retain
rem line above for DOS 2.x should be changed as below
rem 'RETURN %0 %1 .retain'
:create
rem This is the part to RETAIN the current drive:path
rem Creation of C:\RETURN.BAT
rem (explicitely in C:\ in order to have only ONE RETURN.BAT on a system)
if not "%1"==".recreate" if not "%9"=="" goto listfull
echo echo off>C:\RETURN.BAT
if not exist C:\RETURN.BAT goto no_c_return
echo rem --- C:\RETURN.BAT>>C:\RETURN.BAT
echo if %%0==C:\RETURN goto allright>>C:\RETURN.BAT
echo if %%0==c:\return goto allright>>C:\RETURN.BAT
rem 2 lines above for DOS 2.x should be changed as below
rem 'echo if %%0==RETURN goto allright»C:\RETURN.BAT'
rem 'echo if %%0==return goto allright»C:\RETURN.BAT'
rem Build check on unmoved/unrenamed C:\RETURN.BAT into itself and
rem make sure the original C:\RETURN file is being run (if it exists):
rem (this makes command lines longer, which may be undesired and unnecessary)
echo if not exist C:\RETURN.BAT goto NoReturn>>C:\RETURN.BAT
echo C:\RETURN %%1 %%2 %%3>>C:\RETURN.BAT
rem line above for DOS 2.x should be changed as below
rem 'echo RETURN %%1 %%2 %%3>>C:\RETURN.BAT'
echo :NoReturn>>C:\RETURN.BAT
echo echo *** This file C:\RETURN.BAT has been moved or renamed. DON'T! Abort!>>C:\RETURN.BAT
echo echo Execution of this BATch file %%0 may cause erroneous results.>>C:\RETURN.BAT
echo goto end>>C:\RETURN.BAT
echo :allright>>C:\RETURN.BAT
echo if "%%1"=="" %%0 %0 .return .retain>>C:\RETURN.BAT
echo if "%%1"=="*" %%0 %0 .list .retain>>C:\RETURN.BAT
if not "%2"=="" goto store_path_list
echo if "%%3"==".retain" %%0 %0 %%2>>C:\RETURN.BAT
goto add_repl_params
:store_path_list
rem %0 stores the current RETAIN.BAT file name (& called path),
rem which may be renamed, into RETURN.BAT
echo if "%%3"==".retain" %%0 %0 %%2 %2 %3 %4 %5 %6 %7 %8 %9>>C:\RETURN.BAT
rem MANY (LONG) PATHS MAY CAUSE THE LINE ABOVE TO EXCEED 127 CHARACTERS.
rem THIS WILL CREATE UNPREDICTABLE, AND ERRONEOUS RESULTS!!!!!!!!!!!!!!!
:add_repl_params
if "%1"==".recreate" goto recreate
rem COPY adds a line '%1 %2 %3 %4 %5 %6 %7 %8 %9 ' without CRLF to RETURN
rem to be replaced by:
rem 1. the name (& path) of the current RETAIN.BAT file in RETURN;
rem 2. the location of the auxiliary file $RETURN$.AUX or
rem specific processing arguments (beginning with a dot);
rem 3-9. nested path specifications.
copy C:\RETURN.BAT+%1 /a C:\RETURN.BAT /b >nul
rem CD adds the current path (d:\[subdir]) to RETURN to be processed
rem below when RETURN is called.
cd>>C:\RETURN.BAT
echo :end>>C:\RETURN.BAT
echo Type [C:\]RETURN from the DOS-prompt to return to
cd
goto end
:listfull
echo *** The path list is full (8 paths stored). The current path can not be added!
echo No further actions performed.
echo.
echo In order to restart each time with an empty list when the system is booted
echo add the following statement to your AUTOEXEC.BAT file:
echo IF EXIST C:\RETURN.BAT DEL C:\RETURN.BAT (eventually redirect this to NUL)
echo.
:list
echo RETAIN: The paths currently stored are (in chronological order):
echo %2 %3 %4 %5 %6 %7 %8 %9
goto end
:nolist
echo RETAIN: No paths stored currently.
goto end
:no_c_return
echo *** Writing C:\RETURN.BAT not succeeded! No further actions taken!
echo.
echo Very likely there is no C-drive; in that case please manually change
echo all references to C:\ within this BATch file into A:\ or add a command
echo to your AUTOEXEC.BAT file: 'SUBST C: A:\', which after reboot will
echo cause references for drive C: to go to drive A:.
echo.
echo If there is a C-drive it may be write protected or full or the
echo directory entry table for the root of C: is full. Take whatever
echo measure is necessary (enable write, delete a file in C:\ or as above).
goto end
:recreate
rem Like auxiliary file, but without the need to add a path, so ending in CRLF.
echo %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9>>C:\RETURN.BAT
echo :end>>C:\RETURN.BAT
goto end
:RETURN
rem This is the part to return to the retained drive:path
rem Reverse the order of paths stored (%2-%9) in order to process %2
rem only if there are more paths stored, thus if at least %3 exists
if "%3"=="" goto reversed
if "%1"==".reversed" goto reversed
%0 .reversed %9 %8 %7 %6 %5 %4 %3 %2
:reversed
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if %2==%%a:\ goto CDD
rem Otherwise forms as C:\\ (below) may slow down execution with networks.
rem Check existence of %2 (might be removed in the meantime):
if exist %2\*.* goto CDD
rem If command above did not find anything the subdir might exist but be empty.
rem Avoid screen output if redirection below does not succeed: CTTY NUL.
ctty nul
rem write dummy file to the rembered path %2 to check its existence
rem>%2\$0-BYTE$.NUL
ctty con
if exist %2\$0-BYTE$.NUL goto delete
echo The drive:path %2 does not exist (anymore).
echo Or the disk may be write protected. Trying to return to it though.
goto CDD
:delete
rem Delete dummy check file
if exist %2\$0-BYTE$.NUL del %2\$0-BYTE$.NUL
:CDD
rem Set retained drive
%2\
rem Set retained subdirectory
cd %2
echo Returned to
cd
rem Rereverse the order of the remaining paths stored and recreate C:\RETURN
rem without path %2
if not "%3"=="" %0 .recreate %9 %8 %7 %6 %5 %4 %3
rem no more levels nested, thus remove
del C:\RETURN.BAT>nul
goto end
:help
echo --- RETAIN 1.1 (c) 1992, Jim Groeneveld; retains current path up to 8 levels.
echo.
echo Usage: 'RETAIN [aux-path[aux-file]│*│[/│-]?]' from the directory to retain;
echo aux-path = drive:\subdir\ of auxiliary file $RETURN$.AUX, incl. trailing '\';
echo aux-file = the eventual alternate auxiliary file name for $RETURN$.AUX;
echo * = list the currently stored paths (also with 'RETURN *');
echo ? (or /? or -?) = display this help.
echo.
echo DO NOT SPECIFY OTHER PARAMETERS. THIS WILL PRODUCE ERRONEOUS RESULTS!
echo RETAIN may be called successive times to store up to 8 paths.
echo.
echo To return to the very last retained path (drive & subdirectory) just enter
echo 'RETURN' (without any arguments) from the DOS prompt (or CALL RETURN from
echo a BATch file with DOS 3.3&up, or COMMAND /C RETURN with DOS 3.21&down)
echo (the literal command RETURN, not only the RETURN/ENTER-key). This path is
echo removed from the list and RETAIN thus has one more level to store the next
echo time. Enter 'RETURN *' to list the currently stored paths.
goto end
:path SUBROUTINE PATH
shift
:chkpath
rem Check each path entry for the presence of $RETURN$.AUX:
if "%1"=="" goto notfound
rem Check root specifications (ending with a trailing \) firstly: *
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if "%%a:\"=="%1" goto rootpath *
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if "%%a:\"=="%1" goto rootpath *
rem Path spec is not a root *
rem If $RETURN.AUX has been found then restart with location as parameter
if exist %1\$RETURN$.AUX $RETAIN$ %1\$RETURN$.AUX
goto nextpath *
:rootpath *
rem If $RETURN.AUX has been found then restart with location as parameter *
if exist %1$RETURN$.AUX $RETAIN$ %1$RETURN$.AUX
rem goto nextpath *
:nextpath *
rem Check next path entry:
shift
goto chkpath
:notfound
rem path or auxiliary file not found, indicate
$RETAIN$ .notfound
rem Lines marked with * may eventually be deleted all at once resulting in a
rem slightly changed check algorithm: 1. _with_ '\' and 2. _without_ '\'.
rem These lines should work, however, with the * included.
rem goto end
:end
rem delete auxiliary file $RETAIN$.BAT if present
if exist $RETAIN$.BAT del $RETAIN$.BAT