OS: Windows 10, Version: 10.0.17134.112
I was trying to reference a variable that consists of a dynamic variable, which requires delayed expansion, in the form of !string!variable!!
. The problem I'm facing is it is being evaluated as [!string!][variable!!]
rather than ![string][!variable!]!
Here's an example of what I'm trying to do:
@echo off
setlocal EnableDelayedExpansion
::Sets variables - Items to be excluded, and an initial index number to reference these variables
set "_elem1=ex1"
set "_elem2=ex2"
set "_elem3=ex3"
set "_n=1"
::Builds a one-dimensional array excluding some items where item-set is arranged in a specific order
for %%e in (A B C ex1 D E F ex2 G H ex3 I J) do (
if not "!_elem!_n!!"=="%%e" (
set "_array=!_array!!_spc!"%%e""
set "_spc= "
) else (
set /a "_n+=1"
)
)
::Displays actual output
echo %_array%
::Displays DESIRED output
echo "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
pause
Basically, what it does, or at least what it is supposed to do, is it builds a filtered one-dimensional array of items from for
item-set by running a conditional block. If "!_elem!_n!!"
is not equal to "%%e"
, the result for the current iteration is appended to the stored value of _array
variable. Otherwise, it ignores the value from the current iteration and increments the index by 1, effectively changing the value of !_elem!_n!!
on the next iteration.
The problem is variable _n
is dynamic and requires delayed expansion. What I'm trying to accomplish is for the variable !_elem!_n!!
to be evaluated as !
_elem
!_n!
!
rather than !_elem!
_n!!
Upon researching, I've stumbled upon these sources:
The answers provided in the link are really valuable but the index used in the examples are not dynamic, and so, !_elem%_n%!
would be fine in those situations but not if the index is dynamic. Also, a method using call
was provided, which is clever, but call
does not work with if
s.
I'm running out of ideas here. I'd really appreciate any ideas you could throw at this.
Thank you all very much!!