Reading a specific line from a text document
Moderator: DosItHelp
-
- Posts: 1
- Joined: 09 Mar 2016 02:09
Reading a specific line from a text document
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
)
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
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
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
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%
Last edited by sambul35 on 09 Mar 2016 20:21, edited 1 time in total.
Re: Reading a specific line from a text document
If you always look for a line beyond line #1, here is an alternative:
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.
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.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Reading a specific line from a text document
If you want to avoid using a goto to messily break out of a for loop, you can also simply redirect the file.
This will skip the first two lines and set %str% to the value of the third line.
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
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?
Re: Reading a specific line from a text document
On the subject of goto, why not use it to avoid putting potentially many lines in an 'if' block?
vs.
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
I guess because FOR loop will still unduly continue without infamous GOTO.
Re: Reading a specific line from a text document
may be so?lordlaxton wrote:In my program i want to take a specific line (eg. line 2: 7) and set it as a variable.
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