|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
  『楼 主』:
"小函数"来对文件指定行的读取(ReadLine.bat)
使用 LLM 解释/回答一下
经常要对文件的指定行进行读取,特写了一个读取文件指定行的小程序段(ReadLine.Bat),方面以后调用。
使用也比较简单:"Call ReadLine <文件名> <跳过的行数> <读取行数>"就可以了。比如在一个批处理里加上一句"Call ReadLine a.txt 5 7",那么将跳过a.txt文件的前5行,显示下面的7行字符,也包含空行。也可以不指定第三个参数。
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/本程序段需要带参数才能正常运行
echo/&echo/Usage:&echo/Call ReadLine ^<文件名^> ^<跳过行数^> ^<读取行数^>
echo/&echo/例如:call ReadLine aa.txt 5 7 ,将跳过aa.txt文件的前5行,读取下面的7行字符
echo/&echo/如果^<跳过行数^>没有指定,就从文件第一行读取
echo/&echo/指定^<读取行数^>时必须指定^<跳过行^>
echo/======================================
goto :eof
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
|
|
2007-3-19 05:25 |
|
|
baomaboy
银牌会员
    
积分 1513
发帖 554
注册 2005-12-30
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
好啊 不过p看的我头晕,要是再转个VBS的就好了
Okay, but the p is making me dizzy. If there's another VBS one, that would be great.
|
|
2007-3-19 05:28 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
我不知道要是处理N W行的文件效率怎么样
I don't know how efficient it is to handle files with N W lines
|
|
2007-3-19 07:00 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Originally posted by vkill at 2007-3-18 18:00:
我不知道要是处理N W行的文件效率怎么样
兄的这个问题倒没有想过,估计这个小函数只能处理一些小的文本吧,大了就不行啦。
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.
|
|
2007-3-19 07:18 |
|
|
xycoordinate
中级用户
  
积分 493
发帖 228
注册 2007-2-16 来自 安徽
状态 离线
|
|
2007-3-20 00:12 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
支持,顶
|

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'>" |
|
2007-3-20 02:19 |
|
|
tclgb
初级用户
  小子
积分 76
发帖 26
注册 2007-6-20
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
我暂时想不到用版主的文件只读取第一行的方法,所以擅自改动了一下,把参数2和参数3换了一下位置,用ReadLine a.txt 1即可读取第一行,用ReadLine a.txt 1 1读取第二行。谢谢版主,这个文件很好用,同时还望版主见谅。
@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/本程序段需要带参数才能正常运行
echo/&echo/Usage:&echo/Call ReadLine ^<文件名^> ^<读取行数^> ^<跳过行数^>
echo/&echo/例如:call ReadLine aa.txt 5 7 ,将跳过aa.txt文件的前7行,读取下面的5行字符
echo/&echo/如果^<跳过行数^>没有指定,就从文件第一行读取
echo/======================================
goto :eof
Last edited by tclgb on 2007-7-9 at 01:31 PM ]
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 ]
|
|
2007-7-9 13:30 |
|
|
59fei
初级用户
 
积分 60
发帖 23
注册 2007-7-5
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by tclgb at 2007-7-9 01:30 PM:
我暂时想不到用版主的文件只读取第一行的方法,所以擅自改动了一下,把参数2和参数3换了一下位置,用ReadLine a.txt 1即可读取第一行,用ReadLine a.txt ...
跳过行数是零不就能读取第一行了吗?
Skipping zero lines is just to read the first line, right?
|

龙飞电脑网络 中国厨房设备联盟 www.59fei.com
www.chufangshebei.com |
|
2007-7-9 14:55 |
|
|
willsion
高级用户
   
积分 793
发帖 312
注册 2004-9-2
状态 离线
|
|
2007-7-9 15:49 |
|
|
tclgb
初级用户
  小子
积分 76
发帖 26
注册 2007-6-20
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Re:59fei
59fei兄可以运行原文件的ReadLine a.txt 0 1,看有什么提示,在我这里会提示错误
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.
|
|
2007-7-9 21:25 |
|
|
payxl
初级用户
 
积分 26
发帖 14
注册 2007-7-7
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
又学到不少知识
I've learned quite a lot of knowledge again
|
|
2007-7-9 21:32 |
|
|
z060053
新手上路

积分 4
发帖 2
注册 2008-7-25
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
你这个写的有点复杂。其实解决办法可以简化一点。
@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
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
|
|
2008-7-25 14:10 |
|
|
hfxiang
新手上路

积分 1
发帖 1
注册 2018-4-8
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
测试以上各楼层大神的版本,好像用起来总有点不手,擅自改动如下,自用时目前满足所有情况,还请多指教。
:ReadLine <DataFileName> <skipline> <readline>
:: DataFileName 待读入文件
:: skipline 跳过行数,空或非有效数字则默认为0
:: readline 读取行数,空或非有效数字则默认为0,0则表示为剩余的全部
@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 / 本程序段需要带参数才能正常运行
echo/&echo/Usage:&echo/Call ReadLine ^<文件名^> ^<跳过行数^> ^<读取行数^>
echo/&echo / 例如:call ReadLine aa.txt 5 7 ,将跳过 aa.txt 文件的前 5 行,读取下面的 7 行字符
echo/&echo / 如果 ^<跳过行数^> 没有指定,就从文件第一行读取
echo/&echo / 指定 ^<读取行数^> 时必须指定 ^<跳过行^>
echo/======================================
goto :eof
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
|
|
2022-7-4 16:14 |
|