China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 14:37
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » "Small Function" to Read Specified Lines of a File (ReadLine.bat) View 5,394 Replies 12
Original Poster Posted 2007-03-19 05:25 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

  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

@echo off&SetLocal ENABLEDELAYEDEXPANSION
if "%1"=="" (goto --help) else (set file=%~s1)
if "%2"=="" (set first="delims=: tokens=1*") else (set first="skip=%2 delims=: tokens=1*")
if "%3"=="" (
for /f %first% %%a in ('findstr /n .* %file%') do echo/%%b
goto :EOF
)
set last=%3
set loop=0
for /f %first% %%a in ('findstr/n .* %file%') do (
if not defined lxmxn (echo/%%b&set /a loop+=1) else (goto :EOF)
if "!loop!"=="%last%" set lxmxn=Nothing
)
GOTO :EOF

:--help
echo/======================================
echo/This program segment needs to be run with parameters
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 following 7 lines of characters
echo/&echo/If ^<number of lines to skip^> is not specified, it will read from the first line of the file
echo/&echo/When specifying ^<number of lines to read^>, ^<number of lines to skip^> must be specified
echo/======================================
goto :eof
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
baomaboy +3 2007-03-19 05:28
electronixtar +11 2007-03-19 23:37
kcdsw +4 2007-07-09 18:16
Floor 2 Posted 2007-03-19 05:28 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
Okay, but the p is making me dizzy. If there's another VBS one, that would be great.
Floor 3 Posted 2007-03-19 07:00 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
I don't know how efficient it is to handle files with N W lines
Floor 4 Posted 2007-03-19 07:18 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
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.
Floor 5 Posted 2007-03-20 00:12 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
Floor 6 Posted 2007-03-20 02:19 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 7 Posted 2007-07-09 13:30 ·  中国 湖北 武汉 电信
初级用户
小子
Credits 76
Posts 26
Joined 2007-06-20 19:30
19-year member
UID 91913
Gender Male
Status Offline
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.

@echo off&SetLocal ENABLEDELAYEDEXPANSION
if "%1"=="" (goto --help) else (set file=%~s1)
if "%3"=="" (set first="delims=: tokens=1*") else (set first="skip=%3 delims=: tokens=1*")
if "%2"=="" (
for /f %first% %%a in ('findstr /n .* %file%') do echo/%%b
goto :EOF
)
set last=%2
set loop=0
for /f %first% %%a in ('findstr/n .* %file%') do (
if not defined lxmxn (echo/%%b&set /a loop+=1) else (goto :EOF)
if "!loop!"=="%last%" set lxmxn=Nothing
)
GOTO :EOF
:--help
echo/======================================
echo/This program segment needs to be run with parameters
echo/&echo/Usage:&echo/Call ReadLine ^<file name^> ^<number of lines to read^> ^<number of lines to skip^>
echo/&echo/For example: call ReadLine aa.txt 5 7, which will skip the first 7 lines of the aa.txt file and read the following 5 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/======================================
goto :eof


[ Last edited by tclgb on 2007-7-9 at 01:31 PM ]
Floor 8 Posted 2007-07-09 14:55 ·  中国 山东 滨州 联通
初级用户
Credits 60
Posts 23
Joined 2007-07-05 00:44
19-year member
UID 93170
Gender Male
Status Offline
Skipping zero lines is just to read the first line, right?
龙飞电脑网络 中国厨房设备联盟 www.59fei.com
www.chufangshebei.com
Floor 9 Posted 2007-07-09 15:49 ·  中国 广东 深圳 电信
高级用户
★★★
Credits 793
Posts 312
Joined 2004-09-02 00:00
21-year member
UID 31104
Gender Male
Status Offline
They are all experts. ......
Floor 10 Posted 2007-07-09 21:25 ·  中国 湖北 武汉 电信
初级用户
小子
Credits 76
Posts 26
Joined 2007-06-20 19:30
19-year member
UID 91913
Gender Male
Status Offline
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.
Floor 11 Posted 2007-07-09 21:32 ·  中国 甘肃 兰州 电信
初级用户
Credits 26
Posts 14
Joined 2007-07-07 22:54
19-year member
UID 93416
Gender Male
Status Offline
I've learned quite a lot of knowledge again
Floor 12 Posted 2008-07-25 14:10 ·  中国 河南 洛阳 联通
新手上路
Credits 4
Posts 2
Joined 2008-07-25 08:31
18-year member
UID 122050
Gender Male
Status Offline
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
Floor 13 Posted 2022-07-04 16:14 ·  中国 广东 广州 电信
新手上路
Credits 1
Posts 1
Joined 2018-04-08 10:55
8-year member
UID 182752
Gender Male
Status Offline
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
Forum Jump: