JSI Tip 8251. How do I return the last line of a file?

In tip 8250, LINEX can return the last line of a file using:

LINEX -t 1 <FileName


for /f "TOKENS=*" %%a in ('LINEX -t 1 ^<FileName') do (
 set Line=%%a
To return the last line of a file, using standard commands, I have scripted LastLine.bat.

The syntax for using LastLine.bat is:

for /f "TOKENS=*" %%a in ('lastline FileName') do (
 set Line=%%a


for /f "Tokens=1* Delims=:" %%a in ('lastline FileName^|Findstr /l /n /v /c:"*$"') do (
 set Line=%%b
NOTE: See How do I remove the last N lines of a file?

LastLine.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: LastLine FileName&goto :EOF
if not exist %1 echo LastLine: File %1 not found.&goto :EOF
for /f "delims=" %%L in ('find /C /V "" <%1') do (
 set /A offset=%%L -1
 more +!offset! <%1

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.