vbs:lastlinesoftextfile
Read the Last Lines of a Text File
The program works smoothly even on files of twenty thousand lines. It simply read lines, keeping in memory the last N lines. When it reaches the end of the stream, it return the last N lines kept.
- basic
' read all the content of a log file and ' put it into a string. function log2str( byval filename, byval lastLines ) dim fso dim file dim lines() dim linenum dim lastWrittenLine dim out set fso = createobject( "scripting.filesystemObject" ) set file = fso.openTextFile( filename ) redim lines(lastLines) linenum = 0 lastWrittenLine = -1 do while not file.atEndOfStream lines( linenum ) = file.readLine lastWrittenLine = linenum linenum = (linenum + 1 ) mod lastLines loop ' not file.atEndOfStream if lastWrittenLine > 0 then linenum = (lastWrittenLine + 1) mod lastLines do if lines(linenum) <> "" then out = out & lines(linenum) & vbCrLf end if ' lines(linenum) <> "" linenum = (linenum + 1 ) mod lastLines loop until linenum = (lastWrittenLine + 1) end if ' lastWrittenLine ' return all the content log2str = out end function ' log2str
vbs/lastlinesoftextfile.txt · Last modified: 2022/12/02 21:02 by 127.0.0.1