Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Saving & Restoring Your Working Environment

nchernoff

Administrator
May
42
2
Staff member
Migrated From JP Software Wiki

Saving your environment

There are many components to your working environment. There are the environment variables, the current subdirectory on each disk drive, directory history, alias definitions, function definitions, setdos settings, etc. You could even consider the text on the screen part of the environment.

Most of the time, the directories, environment, and directory history are the most important. So these utilities will show how to save and restore those values.

4NT has a concept of a directory stack. PUSHD, POPD, and DIRS are used to save, restore and display the directory stack. Using a similar model, I created PUSHLOCAL, POPLOCAL, and LOCALS. When PUSHLOCAL is run, it will save the current directory history, current directory of each drive, and the environment. You can then change drives, directories, and environment variables and when you POPLOCAL, everything will be restored to the state of the last PUSHLOCAL - i.e. drives, directories, and environment. LOCALS will show you the current depth of saved environments.

It is essentially SETLOCAL/ENDLOCAL for outside of batch scripts.

PushLocal.btm

One of the tricks employed here is to use the volatile environment. The volatile environment is not saved by Windows on a reboot. I create a variable that is uniquely named to the process. The environment variable _PID holds the current process ID. The variable locals%_PID will be unique to each 4NT process. That way each process can have its own LOCALS stack.

PUSHLOCAL creates three unique files.

  • c:\jpsoft\drives%[locals]_%_PID.dat
  • c:\jpsoft\env%[locals]_%_PID.dat
  • c:\jpsoft\dirhist%[locals]_%_PID.dat

Where %[locals] is the current stack depth.

for example:

  • c:\jpsoft\drives1_4176.dat
  • c:\jpsoft\env1_4176.dat
  • c:\jpsoft\dirhist1_4176.dat

Code:
set locals=%@EVAL[%@execstr[set /v locals%_PID] + 1]
cdd /a > c:\jpsoft\drives%[locals]_%_PID.dat
cd >> c:\jpsoft\drives%[locals]_%_PID.dat
set > c:\jpsoft\env%[locals]_%_PID.dat
dirhistory > c:\jpsoft\dirhist%[locals]_%_PID.dat
set /v locals%_PID=%locals


PopLocal.btm

When restoring the environment, you need to remove anything that may have been added. UNSET * will remove the entire environment. Unfortunately it also removes the variable that holds the stack depth too. So, by using the volatile environment in the registry, I can restore that value.

Code:
set locals=%@execstr[set /v locals%_PID]
if "%locals" == "" .or. "%locals" == "0" (echo LOCALS stack is empty & quit)
unset *
set locals=%@execstr[set /v locals%_PID]
for %s in (@c:\JPSOFT\drives%[locals]_%_PID.dat) do cdd %s
dirhistory /r c:\JPSOFT\dirhist%[locals]_%_PID.dat
set /r c:\JPSOFT\env%[locals]_%_PID.dat
del /q c:\JPSOFT\env%[locals]_%_PID.dat c:\JPSOFT\dirhist%[locals]_%_PID.dat c:\JPSOFT\drives%[locals]_%_PID.dat
set /v locals%_PID=%@eval[locals - 1]

Locals.btm

LOCALS will display the depth of currently saved environments.

Code:
set locals=%@execstr[set /v locals%_PID]
iff "%locals" == "" .or. "%locals" == "0" then
  echo LOCALS stack is empty 
else
  echo %locals
endiff
unset locals

Restoring your environment at startup

You can easily adapt the above technique to save and restore your environment when 4NT starts.

In 4EXIT.btm you can add:

Code:
if %_transient != 0 quit
msgbox yesno "Save" Save Drive and Environment Settings?
if %_? != 10 *exit
cdd /a > c:\jpsoft\drives.dat
cd >> c:\jpsoft\drives.dat
set > c:\jpsoft\env.dat
dirhistory > c:\jpsoft\dirhist.dat
history > c:\jpsoft\history.dat

And in 4START.btm:

Code:
if %_pipe != 0 .or. %_transient != 0 quit

set s=Y
inkey /c /k"YN[Enter]" /w10 /x Restore Previous Environment? (Yn) %%s
echo.
iff "%s" eq "Y" .OR. "%s" eq "@28" then
  for %%s in (@c:\JPSOFT\drives.dat) do cdd %%s
  set /r c:\JPSOFT\env.dat
  dirhistory /r c:\JPSOFT\dirhist.dat
  history /r c:\jpsoft\history.dat
endiff
 
Back
Top