How to Use an Environment Variable as an Environment Variable Name

余生颓废 提交于 2019-12-04 11:25:09

You can use a little trick which unfortunately is nowhere documented:

call echo %%%1%%

Then you can use delayed expansion:

setlocal enabledelayedexpansion
echo !%1!

Delayed expansion helps here mostly because it uses other delimiters for the variable and evaluates them directly prior to running the command, while normally the evaluation might clash with normal parameter expansion.

Another way of overdoing this would be a subroutine:

call :meh "echo %%%1%%"

...

:meh
%~1
goto :eof

All examples, including the other answer, have one thing in common here: They all force cmd to evaluate variables/parameters twice. It won't work otherwise, since the first evaluation must produce %VariableName%, while the second will expand that to the variable's contents.

You can find the code also on my SVN.

Anders

B.BAT:

FOR /F "delims=" %%a IN ('echo.%%%1%%') DO set inputvar=%%a
echo %inputvar%

That is one way of doing it.

If all you want to do is echo it, you can do: echo.%%%1%%|more or echo %%%1%%|find /v ""

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!