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

前端 未结 3 535
情话喂你
情话喂你 2021-01-18 22:40

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?

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 23:25

    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
    

提交回复
热议问题