Variable inside variable inside for loop?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Variable inside variable inside for loop?

#1 Post by Sounak@9434 » 04 Jan 2017 04:23

Okay, here comes another problem (or question) from me.
Here I want to expand a variable inside a variable name and then expand the second variable too.
If it was not a for loop I would have used:-

Code: Select all

setlocal enabledelayedexpansion
Set foo=1
Set bar1=main
Echo !bar%foo%!

This code would echo the content of variable bar1.
But in a for loop:-

Code: Select all

setlocal enabledelayedexpansion 
Set var=0
for /f %%a in ('dir /b') do (
 Set var+=1
 Set file%var%=%%a
 Echo !file%var%!
)

This code should set all the file/folder names to different variables and print them but it doesn't because %var% just expands before the command starts executing.
I have found a way to do so by using another 'for /l' loop but that doesn't solves my question, that's just a workaround so, help anyone?
Last edited by Sounak@9434 on 04 Jan 2017 05:10, edited 1 time in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Variable inside variable inside for loop?

#2 Post by Compo » 04 Jan 2017 05:02

Try this:

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion
Set "var=0"
For /F "Delims=" %%A In ('Dir/B') Do (
   Set/A "var+=1"
   Set "file!var!=%%A"
)
Set file
Timeout -1

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Variable inside variable inside for loop?

#3 Post by Sounak@9434 » 04 Jan 2017 05:09

Compo wrote:Try this:

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion
Set "var=0"
For /F "Delims=" %%A In ('Dir/B') Do (
   Set/A "var+=1"
   Set "file!var!=%%A"
)
Set file
Timeout -1

Oops, sorry mistakenly I forgot one line. Sorry ,updated the question.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Variable inside variable inside for loop?

#4 Post by Compo » 04 Jan 2017 05:15

Okay…

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion
Set "var=0"
For /F "Delims=" %%A In ('Dir/B') Do (
   Set/A var+=1
   Set "file!var!=%%A"
   Call Echo=%%file!var!%%
)
Timeout -1

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Variable inside variable inside for loop?

#5 Post by Sounak@9434 » 04 Jan 2017 05:24

Compo wrote:Okay…

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion
Set "var=0"
For /F "Delims=" %%A In ('Dir/B') Do (
   Set/A var+=1
   Set "file!var!=%%A"
   Call Echo=%%file!var!%%
)
Timeout -1

Thanks. :D

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Variable inside variable inside for loop?

#6 Post by aGerman » 04 Jan 2017 05:27

Or ...

Code: Select all

for /f %%B in ("!var!") do Echo !file%%B!


Steffen

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Variable inside variable inside for loop?

#7 Post by Sounak@9434 » 04 Jan 2017 05:32

aGerman wrote:Or ...

Code: Select all

for /f %%B in ("!var!") do Echo !file%%B!


Steffen

Thanks Steffen for this cool one. :mrgreen:

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Variable inside variable inside for loop?

#8 Post by Aacini » 04 Jan 2017 10:32

at Arrays, structures and linked lists in Batch files thread, Aacini wrote:To get the value of an element when the index changes inside FOR/IF, enclose the element in double percent symbols and precede the command with CALL. For example, to move a range of array elements four places to the left:

Code: Select all

for /L %%i in (%start%,1,%end%) do (
   set /A j=%%i + 4
   call set "elem[%%i]=%%elem[!j!]%%"
)

Another way to achieve the previous process is to use an additional FOR command to change the delayed expansion of the index by an equivalent replaceable parameter, and then use the delayed expansion for the array element. This method runs faster than previous CALL:

Code: Select all

for /L %%i in (%start%,1,%end%) do (
   set /A j=%%i + 4
   for %%j in (!j!) do set "elem[%%i]=!elem[%%j]!"
)


Antonio

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Variable inside variable inside for loop?

#9 Post by Sounak@9434 » 04 Jan 2017 11:20

Aacini wrote:
at Arrays, structures and linked lists in Batch files thread, Aacini wrote:To get the value of an element when the index changes inside FOR/IF, enclose the element in double percent symbols and precede the command with CALL. For example, to move a range of array elements four places to the left:

Code: Select all

for /L %%i in (%start%,1,%end%) do (
   set /A j=%%i + 4
   call set "elem[%%i]=%%elem[!j!]%%"
)

Another way to achieve the previous process is to use an additional FOR command to change the delayed expansion of the index by an equivalent replaceable parameter, and then use the delayed expansion for the array element. This method runs faster than previous CALL:

Code: Select all

for /L %%i in (%start%,1,%end%) do (
   set /A j=%%i + 4
   for %%j in (!j!) do set "elem[%%i]=!elem[%%j]!"
)


Antonio

Thanks Antonio. I should've read your thread. Gotta read it now.

Post Reply