0

I want to make a variable Variable, which means that i want to create many variables with variable names, e.g. Var1, Var2, Var3, Var4. This works (see below), but ECHOing was not working since i have to use EnableDelayedExpansion due to single handling within the FOR-Loop and !var%num%! was not interpreted correctly.

So here is what i've got:

SetLocal EnableDelayedExpansion

SET /a num = 0
FOR /F "tokens=*" %%a IN ('dir /b *.bat') DO (
    SET /a num = num + 1
    SET var!num!=%%a
    CALL ECHO No. !num!^: %%var!num!%%
    )

EndLocal DisableDelayedExpansion

After hours, this works now using the CALL-Routine in front of echo

My question to you guys out there is now how to make

    CALL ECHO No. !num!^: %%var!num!%%

a little nicer. I first tried

    ECHO No. !Num!^: !var%num%!

but this fails as it is in on single FOR-Loop. Is there any opportunity to make this nicer than CALLING it?

Thank you in advance Patrick

Rajesh
  • 1,600
  • 5
  • 33
  • 59
Patrick
  • 1
  • 1
  • 2
  • The right term for your "variable within variable" is _array_. See: [arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Mar 04 '15 at 19:24
  • Thanks Rajesh, that was absolutely what i've searched for. – Patrick Mar 10 '15 at 07:12

1 Answers1

3
SetLocal EnableDelayedExpansion

SET /a "num=0"

FOR /F "delims=" %%a IN ('dir /b *.bat') DO (
    SET /a "num+=1"
    SET "var!num!=%%a"
    FOR %%b in (!num!) do ECHO No. !num!: !var%%b!
)

EndLocal 
MC ND
  • 69,615
  • 8
  • 84
  • 126