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 >
DOS Batch File  |  1992-12-10  |  14KB  |  311 lines

  1. echo off
  2.  rem ===RETAIN.BAT, vs. 1.1, 10/12-92===
  3. if "%1"=="" echo --- RETAIN 1.1 (c) 1992, Jim Groeneveld; retains current path up to 8 levels.
  4. if "%1"=="?" goto help
  5. if "%1"=="/?" goto help
  6. if "%1"=="-?" goto help
  7. if "%1"==".path" goto path
  8. if "%1"==".notfound" goto nopath
  9. if "%1"=="*" if exist C:\RETURN.BAT C:\RETURN *
  10.  rem line above for DOS 2.x should be changed as below
  11.  rem 'if "%1"=="*" if exist C:\RETURN.BAT RETURN *'
  12. if "%1"=="*" if not exist C:\RETURN.BAT goto nolist
  13. if "%1"==".list" goto list
  14. if "%1"==".recreate" goto create
  15. if "%1"==".return" goto RETURN
  16. if "%1"==".reversed" goto RETURN
  17. if not "%1"=="" goto RETAIN
  18.   rem %1 is the location or specification of the auxiliary file $RETURN$.AUX
  19.  
  20. :determine
  21.  rem Determination of location (and name) of auxiliary file $RETURN$.AUX in/as:
  22.  rem a. the first and only parameter when calling RETAIN (not checked here)
  23.  rem    1. the full _path_ only, incl. trailing '\' (e.g. 'RETAIN C:\UTIL\') or
  24.  rem    2. the full _file_specification_ (e.g. 'RETAIN C:\UTIL\AUX-FILE.$$$')
  25.  rem b. the environment variable %RETAIN%
  26.  rem    1. the full _path_ only, incl. trailing '\' ('SET RETAIN=C:\UTIL\') or
  27.  rem    2. the full _file_specification_ ('SET RETAIN=C:\UTIL\AUX-FILE.$$$')
  28.  rem c. the ROOT directory of C: (C:\) (or automatically any APPENDed path) or
  29.  rem d. the ROOT directory of the CURRENT drive (\) or
  30.  rem e. the CURRENT directory (.\) or
  31.  rem f. the PARENT directory of the current one (..\) or
  32.  rem g. the SAME directory where RETAIN.BAT is residing:
  33.  rem    if EXPLICITELY specified when starting RETAIN (DOS 3.x&up only).
  34.  rem    (NOT if RETAIN.BAT can only be found via the PATH!) or
  35.  rem h. the DOS PATH variable (this may take quite a while)
  36.  rem If the environment variable has not been set: try other location(s)
  37. if "%RETAIN%"=="" goto no_env
  38.  rem If $RETURN$.AUX found as file specified by %RETAIN% then restart this batch
  39.  rem file (RETAIN.BAT as %0) with _full_path_&_file_name_ as the first parameter
  40. if exist %RETAIN% %0 %RETAIN%
  41.  rem If $RETURN$.AUX found in path specified by %RETAIN% then restart this batch
  42.  rem file (RETAIN.BAT as %0) with _full_path_&_file_name_ as the first parameter
  43. if exist %RETAIN%$RETURN$.AUX %0 %RETAIN%$RETURN$.AUX
  44.  rem If the auxiliary file thus can not be found: try other location(s) below:
  45.  
  46. :no_env
  47.  rem Check the presence of $RETURN$.AUX in (and specify _full_path_&_file_name_
  48.  rem if found):
  49.  rem (the root of C:)
  50. if exist    C:\$RETURN$.AUX %0    C:\$RETURN$.AUX
  51.  rem (the root of the current drive)
  52. if exist      \$RETURN$.AUX %0      \$RETURN$.AUX
  53.  rem (the current directory)
  54. if exist     .\$RETURN$.AUX %0     .\$RETURN$.AUX
  55.  rem (the parent directory)
  56. if exist    ..\$RETURN$.AUX %0    ..\$RETURN$.AUX
  57.  rem (the explicitely specified directory)
  58. if exist %0\..\$RETURN$.AUX %0 %0\..\$RETURN$.AUX
  59.  
  60.  rem ---DOSPATH---
  61.  rem Within the subroutine :path to be called now by subsequent 'shift'ing
  62.  rem the call name for RETAIN (%0) gets lost. To solve this
  63.  rem problem a temporary batch file $RETAIN$.BAT is created initially (in
  64.  rem the current directory) that contains this information hardcoded and
  65.  rem calls RETAIN (or whatever it is renamed) with the necessary parameter:
  66. echo echo off>$RETAIN$.BAT
  67.  rem If no write of this auxiliary file is possible then cancel finally:
  68. if not exist $RETAIN$.BAT goto nopath
  69. echo %0 %%1>>$RETAIN$.BAT
  70.  rem At the end (or somewhere suited) remove the temporary file $RETAIN$.BAT .
  71.  rem Depending on the (path and) name (length) for RETAIN the path environment
  72.  rem variable will be used up to a certain number of characters, maximally
  73.  rem around 100.
  74. %0 .path %path%
  75.  
  76. :nopath
  77. echo *** Necessary auxilary file $RETURN$.AUX could not be found - abort!
  78. echo.
  79. echo     Specify environment variable RETAIN to point to the path where
  80. echo     $RETURN$.AUX resides (either from the DOS prompt or in AUTOEXEC.BAT).
  81. echo     E.g. SET RETAIN=C:\UTIL\             (terminated with backslash).
  82. echo     Or specify full path and file name to the eventual alternate auxiliary
  83. echo     file for $RETURN$.AUX (e.g. SET RETAIN=C:\UTIL\AUX-FILE.$$$).
  84. echo.
  85. echo     Or specify path (terminated with '\') to or full file specification
  86. echo     for $RETURN$.AUX as the first (and only) parameter for %0
  87. goto end
  88.  
  89. :RETAIN
  90. if not "%2"=="" goto auxpath_checked
  91.  rem Now %2 is empty: aux-path[aux-file] %1 specified by user, check existence:
  92.  rem If %1 is _full_path_&_file_name_ of (alternate) $RETURN$.AUX then continue
  93. if exist %1 goto auxpath_checked
  94.  rem If %1 is path only to explicit $RETURN$.AUX then restart RETAIN (%0) with
  95.  rem _full_path_&_file_name_ as the first parameter
  96. if exist %1$RETURN$.AUX %0 %1$RETURN$.AUX
  97.  rem %1 has been specified by the user, but $RETURN$.AUX could not be found:
  98. echo *** Auxiliary path[file] %1 is incorrect, file $RETURN$.AUX not found.
  99. echo     Now trying to find $RETURN$.AUX using path (or full file specification):
  100. echo     1. from environment variable RETAIN (path or full file specification)
  101. echo     2. the root directory of C: (C:\) (or automatically any APPENDed path)
  102. echo     3. the root of the current drive (\)
  103. echo     4. the current directory (.\)
  104. echo     5. the parent directory (..\)
  105. echo     6. the same directory as RETAIN.BAT if RETAIN has been called by path
  106. echo     7. from the DOS PATH variable (this may take quite a while)
  107. echo.
  108. goto determine
  109.  
  110. :auxpath_checked
  111.  rem Check for existence of C:\RETURN.BAT: if so, nesting has to take place.
  112.  rem This is done by adding the current path parameter after the already
  113.  rem present path parameters in C:\RETURN.BAT, where it calls RETAIN.
  114. if not exist C:\RETURN.BAT goto create
  115.  rem %2 (and succeeding parameters) is the retained drive and subdirectory
  116.  rem If path(s) stored are known resume with recreation of C:\RETURN.BAT
  117. if not "%2"=="" goto create
  118.  rem If no stored paths are known (just started RETAIN) force RETURN to
  119.  rem supply the already stored path(s) (to add the current path to).
  120.  rem This returns all currently stored paths as additional parameters up to 8
  121. C:\RETURN %0 %1 .retain
  122.  rem line above for DOS 2.x should be changed as below
  123.  rem 'RETURN %0 %1 .retain'
  124.  
  125. :create
  126.  rem This is the part to RETAIN the current drive:path
  127.  rem Creation of C:\RETURN.BAT
  128.  rem (explicitely in C:\ in order to have only ONE RETURN.BAT on a system)
  129. if not "%1"==".recreate" if not "%9"=="" goto listfull
  130. echo echo off>C:\RETURN.BAT
  131. if not exist C:\RETURN.BAT goto no_c_return
  132. echo rem --- C:\RETURN.BAT>>C:\RETURN.BAT
  133.  
  134. echo if %%0==C:\RETURN goto allright>>C:\RETURN.BAT
  135. echo if %%0==c:\return goto allright>>C:\RETURN.BAT
  136.  rem 2 lines above for DOS 2.x should be changed as below
  137.  rem 'echo if %%0==RETURN goto allright»C:\RETURN.BAT'
  138.  rem 'echo if %%0==return goto allright»C:\RETURN.BAT'
  139.  rem Build check on unmoved/unrenamed C:\RETURN.BAT into itself and
  140.  rem make sure the original C:\RETURN file is being run (if it exists):
  141.  rem (this makes command lines longer, which may be undesired and unnecessary)
  142. echo if not exist C:\RETURN.BAT goto NoReturn>>C:\RETURN.BAT
  143. echo C:\RETURN %%1 %%2 %%3>>C:\RETURN.BAT
  144.  rem line above for DOS 2.x should be changed as below
  145.  rem 'echo RETURN %%1 %%2 %%3>>C:\RETURN.BAT'
  146. echo :NoReturn>>C:\RETURN.BAT
  147. echo echo *** This file C:\RETURN.BAT has been moved or renamed. DON'T! Abort!>>C:\RETURN.BAT
  148. echo echo     Execution of this BATch file %%0 may cause erroneous results.>>C:\RETURN.BAT
  149. echo goto end>>C:\RETURN.BAT
  150.  
  151. echo :allright>>C:\RETURN.BAT
  152. echo if "%%1"=="" %%0 %0 .return .retain>>C:\RETURN.BAT
  153. echo if "%%1"=="*" %%0 %0 .list .retain>>C:\RETURN.BAT
  154. if not "%2"=="" goto store_path_list
  155. echo if "%%3"==".retain" %%0 %0 %%2>>C:\RETURN.BAT
  156. goto add_repl_params
  157.  
  158. :store_path_list
  159.  rem %0 stores the current RETAIN.BAT file name (& called path),
  160.  rem which may be renamed, into RETURN.BAT
  161. echo if "%%3"==".retain" %%0 %0 %%2 %2 %3 %4 %5 %6 %7 %8 %9>>C:\RETURN.BAT
  162.  rem MANY (LONG) PATHS MAY CAUSE THE LINE ABOVE TO EXCEED 127 CHARACTERS.
  163.  rem THIS WILL CREATE UNPREDICTABLE, AND ERRONEOUS RESULTS!!!!!!!!!!!!!!!
  164. :add_repl_params
  165. if "%1"==".recreate" goto recreate
  166.  rem COPY adds a line '%1 %2 %3 %4 %5 %6 %7 %8 %9 ' without CRLF to RETURN
  167.  rem to be replaced by:
  168.  rem 1. the name (& path) of the current RETAIN.BAT file in RETURN;
  169.  rem 2. the location of the auxiliary file $RETURN$.AUX or
  170.  rem    specific processing arguments (beginning with a dot);
  171.  rem 3-9. nested path specifications.
  172. copy C:\RETURN.BAT+%1 /a C:\RETURN.BAT /b >nul
  173.  rem CD adds the current path (d:\[subdir]) to RETURN to be processed
  174.  rem below when RETURN is called.
  175. cd>>C:\RETURN.BAT
  176. echo :end>>C:\RETURN.BAT
  177.  
  178. echo Type [C:\]RETURN from the DOS-prompt to return to
  179. cd
  180. goto end
  181.  
  182. :listfull
  183. echo *** The path list is full (8 paths stored). The current path can not be added!
  184. echo     No further actions performed.
  185. echo.
  186. echo     In order to restart each time with an empty list when the system is booted
  187. echo     add the following statement to your AUTOEXEC.BAT file:
  188. echo     IF EXIST C:\RETURN.BAT DEL C:\RETURN.BAT (eventually redirect this to NUL)
  189. echo.
  190. :list
  191. echo RETAIN: The paths currently stored are (in chronological order):
  192. echo %2 %3 %4 %5 %6 %7 %8 %9
  193. goto end
  194.  
  195. :nolist
  196. echo RETAIN: No paths stored currently.
  197. goto end
  198.  
  199. :no_c_return
  200. echo *** Writing C:\RETURN.BAT not succeeded! No further actions taken!
  201. echo.
  202. echo     Very likely there is no C-drive; in that case please manually change
  203. echo     all references to C:\ within this BATch file into A:\ or add a command
  204. echo     to your AUTOEXEC.BAT file: 'SUBST C: A:\', which after reboot will
  205. echo     cause references for drive C: to go to drive A:.
  206. echo.
  207. echo     If there is a C-drive it may be write protected or full or the
  208. echo     directory entry table for the root of C: is full. Take whatever
  209. echo     measure is necessary (enable write, delete a file in C:\ or as above).
  210. goto end
  211.  
  212. :recreate
  213.  rem Like auxiliary file, but without the need to add a path, so ending in CRLF.
  214. echo %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9>>C:\RETURN.BAT
  215. echo :end>>C:\RETURN.BAT
  216. goto end
  217.  
  218. :RETURN
  219.  rem This is the part to return to the retained drive:path
  220.  rem Reverse the order of paths stored (%2-%9) in order to process %2
  221.  rem only if there are more paths stored, thus if at least %3 exists
  222. if "%3"=="" goto reversed
  223. if "%1"==".reversed" goto reversed
  224. %0 .reversed %9 %8 %7 %6 %5 %4 %3 %2
  225.  
  226. :reversed
  227. 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
  228.  rem Otherwise forms as C:\\ (below) may slow down execution with networks.
  229.  rem Check existence of %2 (might be removed in the meantime):
  230. if exist %2\*.* goto CDD
  231.  rem If command above did not find anything the subdir might exist but be empty.
  232.  rem Avoid screen output if redirection below does not succeed: CTTY NUL.
  233. ctty nul
  234.  rem write dummy file to the rembered path %2 to check its existence
  235. rem>%2\$0-BYTE$.NUL
  236. ctty con
  237. if exist %2\$0-BYTE$.NUL goto delete
  238. echo The drive:path %2 does not exist (anymore).
  239. echo Or the disk may be write protected. Trying to return to it though.
  240. goto CDD
  241.  
  242. :delete
  243.  rem Delete dummy check file
  244. if exist %2\$0-BYTE$.NUL del %2\$0-BYTE$.NUL
  245. :CDD
  246.  rem Set retained drive
  247. %2\
  248.  rem Set retained subdirectory
  249. cd %2
  250. echo Returned to
  251. cd
  252.  
  253.  rem Rereverse the order of the remaining paths stored and recreate C:\RETURN
  254.  rem without path %2
  255. if not "%3"=="" %0 .recreate %9 %8 %7 %6 %5 %4 %3
  256.  rem no more levels nested, thus remove
  257. del C:\RETURN.BAT>nul
  258. goto end
  259.  
  260. :help
  261. echo --- RETAIN 1.1 (c) 1992, Jim Groeneveld; retains current path up to 8 levels.
  262. echo.
  263. echo Usage: 'RETAIN [aux-path[aux-file]│*│[/│-]?]' from the directory to retain;
  264. echo aux-path = drive:\subdir\ of auxiliary file $RETURN$.AUX, incl. trailing '\';
  265. echo aux-file = the eventual alternate auxiliary file name for $RETURN$.AUX;
  266. echo *        = list the currently stored paths (also with 'RETURN *');
  267. echo ? (or /? or -?) = display this help.
  268. echo.
  269. echo DO NOT SPECIFY OTHER PARAMETERS. THIS WILL PRODUCE ERRONEOUS RESULTS!
  270. echo RETAIN may be called successive times to store up to 8 paths.
  271. echo.
  272. echo To return to the very last retained path (drive & subdirectory) just enter
  273. echo 'RETURN' (without any arguments) from the DOS prompt (or CALL RETURN from
  274. echo a BATch file with DOS 3.3&up, or COMMAND /C RETURN with DOS 3.21&down)
  275. echo (the literal command RETURN, not only the RETURN/ENTER-key). This path is
  276. echo removed from the list and RETAIN thus has one more level to store the next
  277. echo time. Enter 'RETURN *' to list the currently stored paths.
  278. goto end
  279.  
  280. :path          SUBROUTINE PATH
  281. shift
  282. :chkpath
  283.  rem Check each path entry for the presence of $RETURN$.AUX:
  284. if "%1"=="" goto notfound
  285.  rem Check root specifications (ending with a trailing \) firstly:             *
  286. 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 *
  287. 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 *
  288.  rem Path spec is not a root                                                   *
  289.  rem If $RETURN.AUX has been found then restart with location as parameter
  290. if exist %1\$RETURN$.AUX $RETAIN$ %1\$RETURN$.AUX
  291. goto nextpath                                                                  *
  292. :rootpath                                                                      *
  293.  rem If $RETURN.AUX has been found then restart with location as parameter     *
  294. if exist %1$RETURN$.AUX $RETAIN$ %1$RETURN$.AUX
  295.  rem goto nextpath                                                             *
  296. :nextpath                                                                      *
  297.  rem Check next path entry:
  298. shift
  299. goto chkpath
  300. :notfound
  301.  rem path or auxiliary file not found, indicate
  302. $RETAIN$ .notfound
  303.  rem Lines marked with * may eventually be deleted all at once resulting in a
  304.  rem slightly changed check algorithm: 1. _with_ '\' and 2. _without_ '\'.
  305.  rem These lines should work, however, with the * included.
  306. rem goto end
  307.  
  308. :end
  309.  rem delete auxiliary file $RETAIN$.BAT if present
  310. if exist $RETAIN$.BAT del $RETAIN$.BAT
  311.