Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a text file line by line and storing it in an array using batch script

I want to read a text file and store each line in an array. When I used the code below, "echo %i%" is printing 0 every time and only array[0] value is getting assigned. But in "set n=%i%",n value is assigned as the last incremented I value.Also "@echo !array[%%i]!" is printing like !array[0]! instead of printing the value. Is there any syntax error in the code?

set /A i=0

for /F %%a in (C:\Users\Admin\Documents\url.txt) do (

set /A i+=1

echo %i%

set array[%i%]=%%a

)

set n=%i%

for /L %%i in (0,1,%n%) do @echo !array[%%i]!
like image 673
NagaLakshmi Avatar asked Sep 18 '13 07:09

NagaLakshmi


1 Answers

Here's a method that is useful at times and very similar to your code:

@echo off
set "file=C:\Users\Admin\Documents\url.txt"
set /A i=0

for /F "usebackq delims=" %%a in ("%file%") do (
set /A i+=1
call echo %%i%%
call set array[%%i%%]=%%a
call set n=%%i%%
)

for /L %%i in (1,1,%n%) do call echo %%array[%%i]%%
like image 160
foxidrive Avatar answered Sep 29 '22 08:09

foxidrive