且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

逐行读取文本文件行,使用批处理脚本将其存储在一个数组

更新时间:2023-08-28 15:31:16

下面是一个在时间有用的,非常相似,你的code的方法:

 关闭@echo
设置文件= C:\\用户\\管理\\文档\\ url.txt
集/ A I = 0FOR / F有usebackq delims =%%一中(%文件%),做(
集/ A I + = 1
调用回声%%我%%
呼叫建立阵列[%%我%% = %%一
呼叫建立N = %%我%%
)对/ L %%我在(1,1,%N%)也叫回声%%阵列[%% I] %%

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]!

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]%%