filebatch-filebatch-processing

How can I read the last 2 lines of a file in batch script


I have a Java program that appends new builds information in the last two lines of a file. How can I read them in batch file?


Solution

  • This will solve the problem, where someFile.txt is the file where you want to read the lines from:

    for /f %%i in ('find /v /c "" ^< someFile.txt') do set /a lines=%%i
    echo %lines%
    set /a startLine=%lines% - 2
    more /e +%startLine% someFile.txt > temp.txt
    set vidx=0
    for /F "tokens=*" %%A in (temp.txt) do (
        SET /A vidx=!vidx! + 1
        set localVar!vidx!=%%A
    )
    echo %localVar1%
    echo %localVar2%
    del temp.txt