在ReadLine.bat的基础上修改了一下,读取文件指定的特定行。
使用方法也比较简单,在批处理中加上 "Call ReadLine2.bat <文件名> <读取行号>" 就可以了,例如:"Call ReadLine2 system.ini 11 5 7 9",可以读取system.ini文件的第5、7、9和11行,后面的行号不分先后大小顺序。但是效率不是很高,读取小文件还是可以的。
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/本程序段需要带参数才能正常运行
echo/&echo/Usage:&echo/Call ReadLine2 ^<文件名^> ^<读取行号^>
echo/&echo/例如:Call ReadLine2 aa.txt 5 7 11 ,将读取aa.txt文件的第5,7,11行
echo/&echo/如果^<读取行号^>没有指定,将读取整个文件的内容
echo/======================================
goto :eof
