Solved How to make a loop print one line of a txt at a time?

February 14, 2014 at 16:56:08
Specs: Windows 7
What Im trying to figure is how to make a loop print or display one line at a time of a .txt file every time the loop restarts.

Eg:
contents of j.txt

aa
bb
cc
dd

Batch file:

setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (j.txt) do (
SET /A vidx=!vidx! + 1
set vard!vidx!=%%A
)
set vard

So from the above code:
%vard1%=aa
%vard2%=bb
%vard3%=cc
%vard4%=dd

Here's the loop part from the same batch:

:loop
set /a m+=2
echo %vard%%m% >>s.txt (This is the line that' needs fixing. I dont know how to make the vard variable change lines from J.txt everytime the loop goes back to the beginning.

The first time the loop runs, it should echo %vard1%%m% to s.txt
The fsecond time the loop runs, it should echo %vard2%%m% to s.txt, and so on.


contents of s.txt should be:

aa2
bb4
cc6
dd8

I know it can be easily by just echoing >> all the lines but I want to know to make a loop do this because I got a thousand lines and a loop would involve much less text. Thanks.


See More: How to make a loop print one line of a txt at a time?

Report •

#1
February 14, 2014 at 17:14:42
✔ Best Answer
Set /a cnt=2
for /F "tokens=*" %%A in (j.txt) do (echo %%A!cnt! && set /a cnt=cnt+1)

please test I've been sick.

::mike


Report •
Related Solutions


Ask Question