Renk Software
Renk Software

Reputation: 174

Display an asked variable in Batch (variable inside a variable)

Is there any way to ask the user which variable wants to be shown and display it? It should be, as a concept, something like this:

@echo off
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
echo %%VAR_TO_SHOW%%

So if we are in "echo %%VAR_TO_SHOW%%" if we entered "VAR_1" I would want to look like this:

echo %VAR_1%

So the output would be "THIS IS VAR_1". Summarizing all what I've said, I would want to make a variable inside a variable. How can I make this?

Another thing I would want to comment is that I've alredy tried "%%!VAR!%%" and "%%VAR%%" without the exclamation marks, but what is displayied is or "%!VAR_TO_SHOW!%" or the same but without (again) the exclamation marks...

Upvotes: 1

Views: 44

Answers (1)

npocmaka
npocmaka

Reputation: 57262

@echo off
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
call echo %%%VAR_TO_SHOW%%%

or

@echo off
setlocal enableDelayedExpansion
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
echo !%VAR_TO_SHOW%!

the second way will work faster.

Upvotes: 3

Related Questions