3

Suppose I have env variables in windows:

IA=C:\a
IB=C:\b

now I write a batch script:(named s.bat)

@echo off
set var=%1
echo %var%

When I run s.bat IA,the result is IA , but I actually want the result to be C:\a. How can I achieve this requirement?

Andreas
  • 5,393
  • 9
  • 44
  • 53
Lion
  • 81
  • 5
  • Use `set var="%IA"`? – Pekka Mar 19 '16 at 08:20
  • The way to achieve this management is described [here](http://stackoverflow.com/questions/35993396/expand-variables-in-variables-with-a-batch-file) or [here](http://stackoverflow.com/questions/35532216/cmd-piping-echo-to-set-expanding-variables-in-variables/35538235#35538235). A detailed explanation of the method used is given [here](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). – Aacini Mar 19 '16 at 13:10

1 Answers1

5

You can use delayed expansion or the call trick.

@echo off
setlocal EnableDelayedExpansion
set "varname=%1"
echo %varname%
echo !%varname%!  - This works
call echo %%varname%%  - This too
jeb
  • 78,592
  • 17
  • 171
  • 225