Patrick
Patrick

Reputation: 1

Windows Batch: For Block with variable within variable

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

Upvotes: 0

Views: 5292

Answers (1)

MC ND
MC ND

Reputation: 70923

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 

Upvotes: 3

Related Questions