Skip navigation

JSI Tip 1097. How do I display the last n lines of a text file?


I have scripted ListTail.bat.

To use it:

ListTail <Drive:>\Folder\FileName.Ext Number

Where Number is the number of none null lines you want to display.

ListTail.bat contains:


@echo off
setlocal
if \{%2\}==\{\} goto syntax
if not exist %1 goto syntax
set /a last=%2
if %last% EQU 0 goto syntax
set /a tot=0
set file=%1
for /f "Tokens=*" %%i in (%file%) do call :total
set /a tot=%tot% - %last%
if %tot% LSS 0 set /a tot=0
set /a cnt=0
for /f "Tokens=1-7* Delims=^>^|^<""" %%i in (%file%) do call :list "%%i" "%%j" "%%k" "%%l" "%%m" "%%n" "%%o" "%%p"
endlocal
goto end
:total
set /a tot=%tot% +1
goto end
:syntax
@echo Syntax: ListTail filename.ext number
endlocal
goto end
:list
set /a cnt=%cnt% + 1
if %cnt% LEQ %tot% goto end
set line=%1%2%3%4%5%6%7%8
set line=%line:"=%
@echo %line%
:end

Hide comments

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.
Publish