中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-14 16:13
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » "Small Function" to Read Specified Lines of a File (ReadLine.bat)
Printable Version  5,642 / 12
Floor1 lxmxn Posted 2007-03-19 05:25
版主 Posts 4,938 Credits 11,386

  Often need to read specified lines of files, so I wrote a small program segment (ReadLine.Bat) to read specified lines of files for future calls.

  It is also relatively simple to use: "Call ReadLine <file name> <number of lines to skip> <number of lines to read>" can be used. For example, adding a sentence "Call ReadLine a.txt 5 7" in a batch file, then it will skip the first 5 lines of the a.txt file and display the following 7 lines of characters, including blank lines. You can also not specify the third parameter.

ReadLine.Bat
Floor2 baomaboy Posted 2007-03-19 05:28
银牌会员 Posts 554 Credits 1,513
Okay, but the p is making me dizzy. If there's another VBS one, that would be great.
Floor3 vkill Posted 2007-03-19 07:00
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
I don't know how efficient it is to handle files with N W lines
Floor4 lxmxn Posted 2007-03-19 07:18
版主 Posts 4,938 Credits 11,386
Originally posted by vkill at 2007-3-18 18:00:
I don't know how efficient it is to handle files with N W lines


  Brother, I haven't thought about this problem. I estimate that this small function can only handle some small text, and it won't work for large ones.
Floor5 xycoordinate Posted 2007-03-20 00:12
中级用户 Posts 228 Credits 493 From 安徽
Floor6 electronixtar Posted 2007-03-20 02:19
铂金会员 Posts 2,672 Credits 7,493
Floor7 tclgb Posted 2007-07-09 13:30
初级用户 Posts 26 Credits 76
I can't think of a way to use the moderator's file to read only the first line for the time being, so I擅自 changed it and swapped the positions of parameter 2 and parameter 3. Using ReadLine a.txt 1 can read the first line, and using ReadLine a.txt 1 1 can read the second line. Thank you, moderator. This file is very useful, and I hope the moderator will forgive me.



[ Last edited by tclgb on 2007-7-9 at 01:31 PM ]
Floor8 59fei Posted 2007-07-09 14:55
初级用户 Posts 23 Credits 60
Skipping zero lines is just to read the first line, right?
Floor9 willsion Posted 2007-07-09 15:49
高级用户 Posts 312 Credits 793
They are all experts. ......
Floor10 tclgb Posted 2007-07-09 21:25
初级用户 Posts 26 Credits 76
Re:59fei

Brother 59fei, you can run the original file's ReadLine a.txt 0 1 and see what prompts come up. It will prompt an error on my side.
Floor11 payxl Posted 2007-07-09 21:32
初级用户 Posts 14 Credits 26
I've learned quite a lot of knowledge again
Floor12 z060053 Posted 2008-07-25 14:10
新手上路 Posts 2 Credits 4
Your input contains some incomplete content. The full correct code should be:

@echo off
setlocal EnableDelayedExpansion
set loop=0
set /a skipline=%2-1
for /f "tokens=1 skip=%skipline% delims=" %%a in (%1) do (
set /a loop=!loop!+1
if "!loop!" leq "%3" (echo %%a) else goto :eof
)
goto :eof

Translated as:

@echo off
setlocal EnableDelayedExpansion
set loop=0
set /a skipline=%2-1
for /f "tokens=1 skip=%skipline% delims=" %%a in (%1) do (
set /a loop=!loop!+1
if "!loop!" leq "%3" (echo %%a) else goto :eof
)
goto :eof
Floor13 hfxiang Posted 2022-07-04 16:14
新手上路 Posts 1 Credits 1
Test the versions of the great gods on each floor above. It seems that it's always a bit uncomfortable to use. I擅自 made the following changes. For personal use, it currently meets all situations. Please give more advice.

:ReadLine <DataFileName> <skipline> <readline>
:: DataFileName File to be read in
:: skipline Number of lines to skip. If empty or not a valid number, it defaults to 0
:: readline Number of lines to read. If empty or not a valid number, it defaults to 0. 0 means all remaining
@echo off&SetLocal ENABLEDELAYEDEXPANSION
if "%1"=="" goto :--help
set /a loop=0
set /a skipline=%~2+0
set /a readline=%~3+0
if %skipline% lss 1 (
set "for_var=usebackq tokens=1 delims="
) else (
set "for_var=usebackq tokens=1 skip=%skipline% delims="
)
if %readline% lss 1 (
set /a step=0
set /a readline=1
) else (
set /a step=1
)
for /f "%for_var%" %%a in ("%~f1") do (
set /a loop+=step
if !loop! leq %readline% (echo %%a) else goto :eof
)
goto :eof
:--help
echo/======================================
echo / This program segment needs parameters to run normally
echo/&echo/Usage:&echo/Call ReadLine ^<File name^> ^<Number of lines to skip^> ^<Number of lines to read^>
echo/&echo / For example: call ReadLine aa.txt 5 7, which will skip the first 5 lines of the aa.txt file and read the next 7 lines of characters
echo/&echo / If ^<Number of lines to skip^> is not specified, it will be read from the first line of the file
echo/&echo / When specifying ^<Number of lines to read^>, you must specify ^<Number of lines to skip^>
echo/======================================
goto :eof
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023