How to create an array from txt file within a batch file?

batch-file

Solution

To create the array:

setlocal EnableDelayedExpansion

set i=0
for /F %%a in (theFile.txt) do (
   set /A i+=1
   set array[!i!]=%%a
)
set n=%i%

To print array elements:

for /L %%i in (1,1,%n%) do echo !array[%%i]!

If you want to pass the array name and lenght as subroutine parameters, then use this way:

call theSub array %n%

:theSub arrayName arrayLen
for /L %%i in (1,1,%2) do echo !%1[%%i]!
exit /B

Problem

I have a txt file with below data ``` aaaa 1000 2000 bbb 3000 4000 cccc 5000 ddd 6000 7000 8000 ``` The numbers of rows in this file are not fixed. I need the first token of each row within an array and to print each element.

Original source