Batch Script - Extract Final N Lines From a File


By: Wahine
January 25, 2012

The following batch script will extract the final N lines from a file:

@echo off
cls
setlocal

:: Display a number of lines from the end of a file (including blank lines).

if {%2} equ {} goto syntax

for /f %%A in ('find /v /c "" ^< "%2"') do (
    set /a lines=%%A-%1
    )

More +%lines% "%2"
exit /b

:syntax
echo Syntax: ^%%1 = number of lines at end of file to be displayed.
echo             ^%%2 = path and input filename.
echo             e.g. 10 C:\Input_filename.txt
echo.
echo Parameter not entered - job terminated.&&exit /b

If the output is to be redirected to a file add >>filename.ext to the More command line.


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question