Page 1 of 1

Reading a specific line from a text document

Posted: 09 Mar 2016 02:17
by lordlaxton
I have a text file that reads something like this (it is located in the same folder as the Bat file).

13
7
46

In my program i want to take a specific line (eg. line 2: 7) and set it as a variable.
I am using this section of code to get the last line in the file but i want to be able to get a specific line how can i modify this code or write another code to do this?

for /f "delims=" %%a in (test.txt) do (

set text=%%a

)

Re: Reading a specific line from a text document

Posted: 09 Mar 2016 03:22
by foxidrive
This uses a native Windows batch script called Findrepl.bat written by Aacini, which uses jscript to make it robust and swift.
viewtopic.php?f=3&t=4697

Place Findrepl.bat in the same folder as the batch file, or in a folder that is on the system path.

There is also copy on Dropbox (unblock it after downloading):
https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat


Code: Select all

@echo off
for /f "delims=" %%a in ('findrepl /o:2:2 ^<test.txt') do set "variable=%%a"
set variable
pause

Re: Reading a specific line from a text document

Posted: 09 Mar 2016 08:48
by sambul35
lordlaxton wrote:In my program i want to take a specific line (eg. line 2: 7) and set it as a variable.


Since FOR /F command evaluates each line's tokens in a text file iteratively line by line from top to bottom, this might work for you:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set count=1
for /f "tokens=*" %%a in (test.txt) do (
if !count! equ 2 (set "text=%%a" & goto :next)
set /a count+=1
)

:next
echo %text%

Re: Reading a specific line from a text document

Posted: 09 Mar 2016 19:14
by Jer
If you always look for a line beyond line #1, here is an alternative:

Code: Select all

@echo off

Set "targetLine=2"
Set "file=test.txt"

set /A "skipTo=%targetLine%-1"
for /f "skip=%skipTo% tokens=*" %%a In (%file%) do set "str=%%a" & GoTo:next
:next
echo found on line %targetLine%: %str%


I use code similar to this to pull a single line from a list, and since I create the list, I add a dummy
line at the top of the text file, then skipping 1 in the for loop would get the first item. In my code
the subtraction is not done.

Re: Reading a specific line from a text document

Posted: 09 Mar 2016 19:29
by ShadowThief
If you want to avoid using a goto to messily break out of a for loop, you can also simply redirect the file.

Code: Select all

@echo off

set lines_to_skip=2
(
   if %lines_to_skip% gtr 0 for /L %%A in (1,1,%lines_to_skip%) do set /p =
   set /p str=
)<test.txt

echo %str%


This will skip the first two lines and set %str% to the value of the third line.

Re: Reading a specific line from a text document

Posted: 10 Mar 2016 06:24
by sambul35
ShadowThief wrote:If you want to avoid using a goto to messily break out of a for loop, you can also simply redirect the file.


I'm curious why do you consider using GOTO from a loop be "messily breakout"? Aren't redirection techniques broadly used in programming since forever? :D

Re: Reading a specific line from a text document

Posted: 10 Mar 2016 10:17
by Jer
On the subject of goto, why not use it to avoid putting potentially many lines in an 'if' block?
if [condition_is_met] goto:conditionMet
do some stuff
do some more stuff...etc.
:conditionMet
continue on

vs.
if [condition_not_met] (
do all the stuff inside the if-block
)

Re: Reading a specific line from a text document

Posted: 10 Mar 2016 10:28
by sambul35
I guess because FOR loop will still unduly continue without infamous GOTO. :cry:

Re: Reading a specific line from a text document

Posted: 13 Mar 2016 06:21
by b0gus
lordlaxton wrote:In my program i want to take a specific line (eg. line 2: 7) and set it as a variable.
may be so?

Code: Select all

@echo off
set /a NTargetLine=2, N=NTargetLine-1
<test.txt (for /f %%v in ('more +%N%') do set "line=%%v" & goto done)
:done
echo.line number %NTargetLine% in test.txt is:%line%
pause
exit