-1

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%

  • 1
    The example you have posted has no purpose in any real batch file, could you please rewirite your question, such that there is a real world task that we can target solutions against. _There is no reason not to use `call :getvarout "%testvar%"` in the above example, or even simpler, `set "varout=%testvar%"`, followed by `echo(%varout%`_. – Compo Jan 07 '21 at 16:20

3 Answers3

0

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:

  • E (escaped % expansion) begins with a value of 1 and increases in proportion to C like so:
  • E = ( C * 4 ) -1
  • The value of C (Calls required to parse the Variable) commences at 0, increments by 1 for the 2nd expansion, Then doubles for Each subsequent Expansion
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • For situations that aren't concerned with the presence of `!` in variables, [this type of solution](https://stackoverflow.com/a/7868477/12343998) is much more appropriate. – T3RR0R Jan 07 '21 at 15:48
0

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
0
@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.

Magoo
  • 77,302
  • 8
  • 62
  • 84