Indirect Access to Environment Variables (by reference) |
You may wish to indirectly read/write to an environment variable whose name is (or can be) contained by another environment variable.
You will almost always want to do this inside a subroutine so that is what this code shows:
@echo off setlocal ENABLEDELAYEDEXPANSION set COUNTER=11 call :ADD1 "COUNTER" echo TEST: COUNTERS new value is "%COUNTER%" (should be "12") goto :EOF @rem ### Manipulate value where only variable's name is passed ######### :Add1 @rem *** Get Passed variable name and its value *** set VarName=%~1 set VarValue=!%VarName%! echo TEST: VARNAME "%VarName%" has a value of "%VarValue%" @rem *** Change the value (we will return it) *** set /a VarValue=VarValue + 1 @rem *** "Return" the value *** set %VarName%=%VarValue% goto :EOF