Joshi
Joshi

Reputation: 631

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?

Upvotes: 6

Views: 19763

Answers (4)

Smeterlink
Smeterlink

Reputation: 786

This is a modified answer of @joshi since their lines don't work:

echo %localVar1%
echo %localVar2%

So it's like this

@echo off
for /f %%i in ('find /v /c "" ^< %1') do set /a lines=%%i
::echo %lines%
set /a startLine=%lines% - %2
more /e +%startLine% %1 | find /v ""

Call it with Tail.bat file lines. The last call of find prevents more to keep the file opened when doesn't fit the screen, so find forces to print out everything from stdin to stdout.

Upvotes: -1

npocmaka
npocmaka

Reputation: 57252

::change the values bellow with a relevant ones.
set "file=C:\some.file"
set "last_lines=2"


for /f %%a in ('findstr /R /N "^" "%file%" ^| find /C ":"') do @set lines=%%a
set /a m=lines-last_line
more +%m% "%file%"

Directly from the command line:

C:\>set "file=C:\some.file"
C:\>set "last_lines=5"
C:\>(for /f %a in ('findstr /R /N "^" "%file%" ^| find /C ":"') do @set lines=%a)&@set /a m=lines-last_lines&call more +%m% "%file%"

Upvotes: 1

Joshi
Joshi

Reputation: 631

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

Upvotes: 3

Aacini
Aacini

Reputation: 67216

This code segment do the trick...

for /F "delims=" %%a in (someFile.txt) do (
   set "lastButOne=!lastLine!"
   set "lastLine=%%a"
)
echo %lastButOne%
echo %lastLine%

EDIT: Complete TAIL.BAT added

This method may be modified in order to get a larger number of lines, that may be specified by a parameter. The file below is tail.bat:

@echo off
setlocal EnableDelayedExpansion

rem Tail command in pure Batch: Tail.bat filename numOfLines
rem Antonio Perez Ayala

for /F "delims=" %%a in (%1) do (
   set /A i=%2, j=%2-1
   for /L %%j in (!j!,-1,1) do (
      set "lastLine[!i!]=!lastLine[%%j]!
      set /A i-=1
   )
   set "lastLine[1]=%%a"
)
for /L %%i in (%2,-1,1) do if defined lastLine[%%i] echo !lastLine[%%i]!

2ND EDIT: New version of TAIL.BAT added

The version below is more efficient:

@echo off
setlocal EnableDelayedExpansion

rem Tail command in pure Batch, version 2: Tail.bat filename numOfLines
rem Antonio Perez Ayala

set /A firstTail=1, lastTail=0
for /F "delims=" %%a in (%1) do (
   set /A lastTail+=1, lines=lastTail-firstTail+1
   set "lastLine[!lastTail!]=%%a"
   if !lines! gtr %2 (
      set "lastLine[!firstTail!]="
      set /A firstTail+=1
   )
)
for /L %%i in (%firstTail%,1,%lastTail%) do echo !lastLine[%%i]!

Upvotes: 7

Related Questions