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!
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.
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
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
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.
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