Pablo
Pablo

Reputation: 1

Can´t show element of an array in a batch file

I know this question has been answered a lot already but I´ve been looking all of the answers and I thought that I had it right this time but I still can´t echo an element of an array using a variable as the index number. Here´s the code:

setlocal enableDelayedExpansion
set Dir=C:\Users\ADMIN\Downloads
set /a counter=0
for /f %%F in ('dir /b "%profDir%"') do echo %%F>>"%profDir%\temp.txt"
for /f %%F in (%profDir%\temp.txt) do (set m[!counter!]=%%F & set /a counter+=1)
echo !m[%counter%]!
pause

The temp file has all the file names of the directory I do the search in, but I want to use the "array" for other tasks later, and am unable to continue until I finally get this right.

Thanks.

Upvotes: 0

Views: 41

Answers (1)

Magoo
Magoo

Reputation: 80013

Since you increment counter after having assigned the value, the value of counter after the second for is the next array-entry that would have been created.

Try using

set m[

to display all of the values that have been set as they appear in the environment

or

set /a kounter=counter-1
for /l %%a in (0,1,%kounter%) do echo !m[%%a]!
for /l %%a in (0,1,%kounter%) do call echo %%m[%%a]%%

to list them

[note: fixed %m[%%a]% to !m[%%a]! and added further display method (call...)]

Upvotes: 1

Related Questions