Script to copy last 10 lines from text file

email me

VBScript

Const FOR_READING = 1

Dim objFs, objFile, strFileName, LinesToReturn, i, aryLine, j

strFileName = “test.txt”
LinesToReturn = “10”
Set objFs = WScript.CreateObject(“Scripting.FileSystemObject”)
If objFs.FileExists(strFileName) Then
Set objFile = objFs.OpenTextFile(strFileName, FOR_READING)
ReDim aryLine(LinesToReturn)
i = 0
Do Until objFile.AtEndOfStream
i = (i+1) Mod LinesToReturn
aryLine(i) = objFile.ReadLine
Loop
For j = 1 To LinesToReturn
If aryLine(j) = vbEmpty Then Exit For
WScript.Echo aryLine(j)
Next
End If

 

Batch

@echo off > NFILE & setLocal enableDELAYedeXpansioN

:main
for /f “tokens=1* delims=[]” %%a in (‘find /n /v “” ^< MFILE’) do (
set LAST=%%a
)

set/a FIRST=LAST-9

for /f “tokens=1* delims=[]” %%a in (‘find /n /v “” ^< MFile’) do ( if %%a geq !FIRST! echo.%%b ) >> NFILE
goto :eof