3

I am trying to echo the VALUE of a variable whose name contains another variable ($project!$project_number!_control) while using delayed expansion. I can't figure out the syntax to do this. Here's the code:

@echo off
setlocal enabledelayedexpansion
set $loop_counter=
for %%g in (1,2,3,4,5,6,7,8) do (
    set /a $loop_counter+=1
    if !$loop_counter! gtr 4 (
        echo.
        rem echo $loop_counter = !$loop_counter!
        set /a $project_number=!$loop_counter!-4
        echo $project_number = !$project_number!
        set $project!$project_number!_control=Project Description # !$loop_counter!
        echo $project!$project_number!_control = !$project!$project_number!_control!
    )
)
echo.
pause
echo.
set
echo.
pause

The line of code I'm having the problem with is the last line in the for loop:

echo $project!$project_number!_control = !$project!$project_number!_control!

Specifically, it's the code to the right of the equal sign that echoes the VALUE of the $project!$project_number!_control variable that I'm having trouble with. The set command (that is run after the pause command in the code above) clearly shows that the values "Project Description #5," "Project Description #6," "Project Description #7," and "Project Description #8" were assigned correctly to the corresponding project1_control, project2_control, project3_control, and project4_control, variables.

Can someone please tell me what syntax to use to display the VALUE of the $project!$project_number!_control variable?

Thanks In Advance For The Help!

Bill Vallance
  • 227
  • 2
  • 8

1 Answers1

1

The solution to this problem is that the $project!$project_number!_control variable must undergo double expansion in order to return its value. Exclamation marks cannot be used more than once with a variable when delayed expansion is enabled as I had done in my original code, below:

!$project!$project_number!_control!

Instead or the exclamation marks placed at the beginning and end of the variable as shown above, double percent signs (%%) should be placed at the beginning and end of the variable as follows:

%%$project!$project_number!_control%%

When delayed expansion takes place this statement will expand to:

%$project!$project_number!_control%

As can be seen in the expanded variable code above, the VALUE of the variable is still not returned. To do that a SECOND expansion must take place. To execute a second variable expansion, the call statement is placed in front of the echo statement at the beginning of the last line of code in the for loop. The corrected last line of code in the for loop is:

call echo $project!$project_number!_control = %%$project!$project_number!_control%%

When the code executes with the corrected line of code everything works as expected. The entire corrected code listing is now:

@echo off
setlocal enabledelayedexpansion
set $loop_counter=
for %%g in (1,2,3,4,5,6,7,8) do (
    set /a $loop_counter+=1
    if !$loop_counter! gtr 4 (
        echo.
        rem echo $loop_counter = !$loop_counter!
        set /a $project_number=!$loop_counter!-4
        echo $project_number = !$project_number!
        set $project!$project_number!_control=Project Description # !$loop_counter!
        call echo $project!$project_number!_control = %%$project!$project_number!_control%%
    )
)
echo.
pause
echo.
set
echo.
pause

I hope this helps someone who has been struggling with using WIndows Command numbered variables. Remember, the call statement is the key to executing a second expansion when delayed expansion is enabled.

Bill Vallance
  • 227
  • 2
  • 8