"tail -1" equivalent in Windows?

Posted on 2007-10-14
Last Modified: 2012-06-22
Is there a way to read the last line of text file using a Windows batch file?  I essentially want to use the equivalent of "tail -1" in *NIX.  I eventually want to read the last line of each file in a specific subdirectory, or read the last line of a file on a group of workstations.
Question by:jclingensmith
    LVL 30

    Expert Comment

    You could do this:

    @echo off


    set lastLine=

    for /f "tokens=*" %%a in ('type "%~1"') do set lastLine=%%a

    echo Last line: %lastLine%

    This batch file will accept the name of the file where you want the last line as a command line parameter.

    You could adapt this processing to read the last line of all files in a directory like so:

    @echo off


    set workDir=.

    if not "%~1"=="" set workDir=%~1
    if not exist "%workDir%" echo %~1 does not exist&goto :EOF

    pushd "%workDir%"

    for /f "tokens=*" %%a in ('dir /b /a-d') do call :GETLASTLINE "%%a"


    goto :EOF


    set lastLine=

    for /f "tokens=*" %%a in ('type "%~1"') do set lastLine=%%a

    echo File: %~1 Last line: %lastLine%
    LVL 23

    Expert Comment

    install Cygwin and then ue the tail command
    LVL 23

    Accepted Solution

    windows apparently does have a tail command, it comes as part of a resource kit that you can install on XP:
    LVL 9

    Expert Comment

    here's a simple vbscript to simulate tail.

    On Error Resume Next
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set wshArgs = WScript.Arguments
    If wshArgs(0) = "" Or wshArgs(1) = "" Then
          WScript.Echo "Usage: cscript /nologo " & Wscript.ScriptName & " <tail number> filename "
    End If
    myTailNum = wshArgs(0)
    myFile = wshArgs(1)
    Set objFile = objFSO.OpenTextFile(myFile,1)
    For k=0 To myTailNum
    Do Until objFile.AtEndOfStream
          WScript.Echo objFile.ReadLine

    usage: cscript /nologo tail.vbs 10 file
    LVL 4

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now