JSI Tip 4574. How can I count the number of lines in a file, and/or display the last n lines?

I have scripted Tail.bat to perform both functions.

The syntax is:

tail FileName \[NumberOfLinesToDisplay\]

If NumberOfLinesToDisplay is 0, you can set the file line count by using:

for /f %%i in ('tail FileName 0') do set lines=%%i

If NumberOfLinesToDisplay is omitted, 10 lines will be displayed.

If NumberOfLinesToDisplay is greater than the lines in the file, all lines in the file will be displayed.

Tail.bat contains:

@echo off
if \{%1\}

\{\} @echo FileName parameter requied.&goto :EOF if not exist %1 @echo %1 does NOT exist.&goto :EOF setlocal set file=%1 set /a number=10 if not \{%2\}

\{\} set /a number=%2 for /f %%i in ('find /v /c "" ^

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.