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

Jerold Schulman

December 17, 2001

1 Min Read
ITPro Today logo in a gray background | ITPro Today


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 offif {%1}
{} @echo FileName parameter requied.&goto :EOFif not exist %1 @echo %1 does NOT exist.&goto :EOF setlocalset file=%1set /a number=10if not {%2}
{} set /a number=%2for /f %%i in ('find /v /c "" ^

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like