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-06-20 11:46
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » "Small Function" to Read Specific Lines of a File (ReadLine2.bat) View 3,010 Replies 5
Original Poster Posted 2007-03-20 03:34 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Modified based on ReadLine.bat to read specific lines from a file.

  The usage is also relatively simple. Add "Call ReadLine2.bat <file name> <line number to read>" in the batch script. For example: "Call ReadLine2 system.ini 11 5 7 9" can read lines 5, 7, 9, and 11 of the system.ini file. The line numbers behind do not have a strict order of size. But the efficiency is not very high, and it is still acceptable for reading small files.

ReadLine2.bat

@echo off&SetLocal ENABLEDELAYEDEXPANSION
:::::::::::ReadLine2.Bat::::::::::::::::::

if "%1"=="" (goto --help) else (set file=%1)
if not exist %file% goto :eof
if "%2"=="" (
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%file%"') do echo/%%b
goto :eof
) else (
set args=%*
for %%a in (!args!) do (
if not "%%a"=="%1" (for /f "tokens=1* delims=:" %%b in ('findstr /n .* "%file%"') do (
if "%%b"=="%%a" echo/%%c)
)
)
)
goto :eof

:--help
echo/======================================
echo/This program segment needs parameters to run normally
echo/&echo/Usage:&echo/Call ReadLine2 ^<file name^> ^<line number to read^>
echo/&echo/For example: Call ReadLine2 aa.txt 5 7 11, which will read lines 5, 7, 11 of the aa.txt file
echo/&echo/If ^<line number to read^> is not specified, the entire content of the file will be read
echo/======================================
goto :eof
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
baomaboy +4 2007-03-20 03:51
29984365 +1 2007-04-07 04:41
HUNRYBECKY +3 2008-07-30 08:45
Floor 2 Posted 2007-03-20 03:51 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
Bookmarked
Floor 3 Posted 2007-03-20 21:33 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Moderator lxmxn, why did you post in two places? For the convenience of management, it is suggested to move it under the original ReadLine1.bat. Thanks
Floor 4 Posted 2007-03-20 23:17 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by HUNRYBECKY at 2007-3-20 08:33:
Why did the moderator lxmxn post the same content in two places? For the convenience of management, it is suggested to move it to the original ReadLine1.bat. Thanks


  Isn't this called "posting in two places"?

  Although both are used to read specified lines of a file, their specific functions are different. One is to read consecutive lines, and the other is to read specific lines, and the effects are also different.
Floor 5 Posted 2007-04-07 04:42 ·  中国 浙江 杭州 电信
初级用户
Credits 39
Posts 15
Joined 2007-04-04 04:40
19-year member
UID 83902
Gender Male
Status Offline
Floor 6 Posted 2008-07-25 14:07 ·  中国 河南 洛阳 联通
新手上路
Credits 4
Posts 2
Joined 2008-07-25 08:31
17-year member
UID 122050
Gender Male
Status Offline
@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 :eo
Forum Jump: