1

I want to make a batch program with will display every 5th line of a text file, like line no 1, 6, 11, 16 .... I tried modifying head.bat code found here: Windows batch command(s) to read first line from text file

My code is like below:

@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

This code is not working. Can you please tell me what is the wrong with this code?

Community
  • 1
  • 1
chanchal1987
  • 2,320
  • 7
  • 31
  • 64

1 Answers1

1

Seems like you need to change one line in your script as follows:

@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

Now the script should work.

Andriy M
  • 76,112
  • 17
  • 94
  • 154