The title may be confusing but heres what im trying to do:
@echo off
set testvar=Hello
call :getvarout testvar
:getvarout
set varout=%~1
echo %%%varout%%%
pause
Expected Output:
Hello
Output:
%testvar%
The title may be confusing but heres what im trying to do:
@echo off
set testvar=Hello
call :getvarout testvar
:getvarout
set varout=%~1
echo %%%varout%%%
pause
Expected Output:
Hello
Output:
%testvar%
you can either use delayed expansion to allow expansion of nested variables using like so:
Setlocal enabledelayedexpansion
Echo(!%varout%!
Or use the Call
command in conjunction with Echo
to force the parser to undergo an additional round of expansion using:
Call Echo(%%varout%%
The above call method is rather inefficient, however if the variables may contain the exclamation character it is an alternative method.
With the use of Call, A variable can be expanded at increasing levels of depth, until the variable is found to be empty. The limitation is in the line length limit, as the number of % expansions that need to be used to expand the variable increases proportionally to the number of Calls used to Parse over the variable for each level of expansion to be achieved.
The following rules apply to expansion depth using Call:
Do you have in mind something similar to this?
@echo off
set testvar=Hello
call :getvarout testvar
goto end
:getvarout
set varout=%~1
call echo %%%varout%%%
pause
:end
@ECHO OFF
SETLOCAL
set "testvar=Hello"
call :getvarout testvar
ECHO varout value is "%varout%"
GOTO :eof
:getvarout
set varout=%~1
CALL SET "varout=%%%varout%%%"
GOTO :EOF
The object of the exercise is, I believe, to retrieve the value of a variable whose name is known - or whose name is contained in another variable, so
call :getvarout %testvar%
would retrieve the value of the variable hello
.
For OP: When you use the point-click-and-giggle method of executing a batch, the batch window will often close if a syntax-error is found. You should instead open a 'command prompt' and run your batch from there so that the window remains open and any error message will be displayed.
Use set "var1=data"
for setting values - this avoids problems caused by trailing spaces. In comparisons, use if "thing1" == "thing2" ...
to avoid problems caused by spaces in thing1/2.
On original code:
Unlike many languages, batch has no concept of the end of a "procedure" - it simply continues execution line-by-line until it reaches the end-of-file. Consequently, you need to goto :eof
after completing the mainline, otherwise execution will continue through the subroutine code. :EOF
is a predefined label understood by CMD
to mean end of file
. The colon is required.