使用Batch Script读取每5行

时间:2011-06-24 17:43:45

标签: windows file-io batch-file

我想创建一个批处理程序,将显示文本文件的每第5行,如第1,6,11,16行.... 我尝试修改此处的head.bat代码:Windows batch command(s) to read first line from text file

我的代码如下:

@echo off
setlocal enabledelayedexpansion
if [%1] == [] goto usage

SET /a counter=0

for /f "usebackq delims=" %%a in (%1) do (
set /a testcond=(%%counter-1)%4
if "!testcond!"=="0" echo %%a
set /a counter+=1
)

goto exit

:usage
echo Usage: fifth FILENAME

:exit

此代码无效。你能告诉我这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

似乎您需要更改脚本中的一行,如下所示:

@echo off
setlocal enabledelayedexpansion
if [%1] == [] goto usage

SET /a counter=0

for /f "usebackq delims=" %%a in (%1) do (
set /a "testcond=(counter-1)%%5"
if "!testcond!"=="0" echo %%a
set /a counter+=1
)

goto exit

:usage
echo Usage: fifth FILENAME

:exit

现在脚本应该可以工作。