Back to home page

DOS ain't dead

Forum index page

Log in | Register

Back to the forum
Board view  Mix view

save & restore environment (Developers)

posted by bencollver Homepage, 28.02.2026, 04:26

> 4DOS and NT's CMD both have "SETLOCAL". So if you don't need any vars to
> persist, try that.

Thanks for that information. 4DOS is really "where it's at" isn't it?

> If you insist on this way (presumably to run on older shells), I'd suggest
> putting it in a single .BAT like "SAVEENV.BAT [on | off]". The AWK scripts
> are small enough to be in-place, but even an embedded AWK script (or ten)
> is easy to use.

Here's an attempt at SAVEENV.BAT:

@echo off

if x%1 == xon goto save
if x%1 == xoff goto restore
goto usage

:usage
echo SAVEENV.BAT ON
echo This saves the environment.
echo.
echo SAVEENV.BAT OFF
echo This restores the saved environment.
echo.
goto the_end

:save

echo BEGIN{ >%TEMP%\envset.awk
echo print "@echo off" >>%TEMP%\envset.awk
echo for(k in ENVIRON){printf "SET %%s=%%s\n",k,ENVIRON[k]} >>%TEMP%\envset.awk
echo } >>%TEMP%\envset.awk

awk -f %TEMP%\envset.awk >%TEMP%\envset.bat

goto the_end

:restore

echo BEGIN{ >%TEMP%\envdel.awk
echo print "@echo off" >>%TEMP%\envdel.awk
echo for(k in ENVIRON){printf "SET %%s=\n",k} >>%TEMP%\envdel.awk
echo } >>%TEMP%\envdel.awk

awk -f %TEMP%\envdel.awk >%TEMP%\envdel.bat

call %TEMP%\envdel.bat
call %TEMP%\envset.bat

goto the_end

:the_end

 

Complete thread:

Back to the forum
Board view  Mix view
23247 Postings in 2191 Threads, 405 registered users (0 online)
DOS ain't dead | Admin contact
RSS Feed
powered by my little forum