I use "SetLocal/EndLocal" a lot to avoid "polluting" the environment - and these can often be nested somewhat deeply as batch files call other batch files. However, there are times when I want to set a variable in an upper environment, or even the root environment (i. e. that of the TCMD/TCC session in which I am running the batch file), and the only way I've found to do this is the (fairly long and complicated) code sequence:
REM
REM Where I am setting the variable:
REM
...
Set Variable=...
...
REM Set variable as a volatile variable in the registry
Set /V Variable=%Variable
REM
REM Where I am retrieving the variable's value:
REM
REM Retrieve variable from registry
REM (Is this the only way????)
Set Variable=%@ExecStr[Set /V Variable]
DoWhatever with %Variable as needed...
REM Drop variable from the registry (assuming this even works)
Set /V Variable=
Possibilities:
"/U#": Set in the environment # levels up from the current environment (may not be generally useful...)
"/U": Set in the environment one level up. (Specifying without a count - one level up - may be generally useful...)
"/T" or "/R": Set in the "Top" or "Root" environment...
I know I can avoid using SetLocal/EndLocal commands and drop the batch-file variables I no longer need at the bottom of the batch file, but doing this for all no-longer-needed variables that were used and avoiding variable conflicts with the "current" envirionment is more effort/discipline than I really want to put into this. (If setting a variable in a "lower" environment that was defined in an "upper" environment didn't "re-create" the variable in the "lower" environment (again, like other languages that have variable scoping) would do the job, but I know that this would probably "break" too much existing code and is therefore too much to ask.)
I do this regularly in C Plus Plus programs (and I am refering to environment variables) so I absolutely know it can be done...
<O:p
REM
REM Where I am setting the variable:
REM
...
Set Variable=...
...
REM Set variable as a volatile variable in the registry
Set /V Variable=%Variable
REM
REM Where I am retrieving the variable's value:
REM
REM Retrieve variable from registry
REM (Is this the only way????)
Set Variable=%@ExecStr[Set /V Variable]
DoWhatever with %Variable as needed...
REM Drop variable from the registry (assuming this even works)
Set /V Variable=
Possibilities:
"/U#": Set in the environment # levels up from the current environment (may not be generally useful...)
"/U": Set in the environment one level up. (Specifying without a count - one level up - may be generally useful...)
"/T" or "/R": Set in the "Top" or "Root" environment...
I know I can avoid using SetLocal/EndLocal commands and drop the batch-file variables I no longer need at the bottom of the batch file, but doing this for all no-longer-needed variables that were used and avoiding variable conflicts with the "current" envirionment is more effort/discipline than I really want to put into this. (If setting a variable in a "lower" environment that was defined in an "upper" environment didn't "re-create" the variable in the "lower" environment (again, like other languages that have variable scoping) would do the job, but I know that this would probably "break" too much existing code and is therefore too much to ask.)
I do this regularly in C Plus Plus programs (and I am refering to environment variables) so I absolutely know it can be done...
<O:p