1

I have to read the 5th last line from log file through a vb script. Can somebody please guide me how to do it?

I am using following code which only reads the last line and echo last line and line count, need to enhance it.

Dim fh, fso, sLastLine, lineCount

Set fso = CreateObject("Scripting.FileSystemObject")
Set fh = fso.OpenTextFile( "E:\Coding\VB\demo.txt" )
sLastLine = ""
lineCount = 0
Do Until fh.AtEndOfStream
    lineCount = lineCount + 1
    sLastLine =  fh.ReadLine()
Loop
fh.Close

WScript.Echo sLastLine
WScript.Echo lineCount

Content for demo.txt is as follows:

zcZcxZCsdfdfsfd
aaaaa
bbbb
cccc
dddd
eeee

In the end there are 4 blank lines and i have to read the "eeee" here.

brettdj
  • 54,857
  • 16
  • 114
  • 177
  • Reading a file is common practice *(answered many times here)*, give it a go and when you have a specific issue with your code come back and we will try and answer it. – user692942 Feb 22 '17 at 13:35

3 Answers3

2

One simple solution is to use the System.Collections.Queue from the .net framework. It works, it is simple and won't require huge amount of RAM because lines of text are discarded as they are read but it does require the .net framework to be installed. Any version of the .net framework will be fine.

Option Explicit

Const LastLinesCount = 5

Dim DemoFile : Set DemoFile = CreateObject("Scripting.FileSystemObject").OpenTextFile("demo.txt")
Dim Queue : Set Queue = CreateObject("System.Collections.Queue")
Dim i

Do While Not DemoFile.AtEndOfStream
    Queue.Enqueue DemoFile.ReadLine
    If Queue.Count > LastLinesCount then Queue.Dequeue
Loop

For i = 1 to LastLinesCount
    WScript.echo Queue.Dequeue
Next

DemoFile.Close
Regis Desrosiers
  • 537
  • 3
  • 13
  • Why `Close()` the first stream when you could just set `.Position = 0` and start reading it again? – user692942 Feb 22 '17 at 13:30
  • Because the TextStream object provided FileSystemObject does not have a Position property. – Regis Desrosiers Feb 22 '17 at 16:38
  • Your absolutely right was thinking of the `ADODB.Stream` object. – user692942 Feb 22 '17 at 17:12
  • But there would be problem with your suggested approach if the logs are written to the 'demo.txt' after the first read of the complete file. Can it be done in a single read? – bharat vivek Feb 23 '17 at 01:52
  • bharat vivek raised a valid concern so I rewrote it to do everything in a single pass. BTW, I do not agree that this question has been answered with the "Reading a certain line of a text file in vbscript" post because this answer requires to know the number of lines in the input file which is not known. – Regis Desrosiers Feb 24 '17 at 12:49
  • @RegisDesrosiers Maybe but it's the exact same principle, just because the number of lines is different doesn't make it anymore unique. – user692942 Feb 25 '17 at 08:22
  • @Lankymart In the example, the input file has 6 lines and we want the last 5 lines. What you say is that we can call ExtractLinesFromTextFile(Myfile,2,6). It is not possible because you do not know total number of lines (6) until you read the file. The input file will not always have 6 lines. – Regis Desrosiers Feb 25 '17 at 10:05
  • @RegisDesrosiers look, I'm not going to argue with you I even think your most recent edit is a decent answer *(hence the +1)*. None of that though detracts from the fact the question is just *"read a file by line"* by another name, it doesn't matter how many lines or which lines. I've seen enough answers that start `Do While Not someFile.AtEndOfStream` to know this is a dup. – user692942 Feb 25 '17 at 11:28
0

Sounds like you can trim the end and get the last line (not tested):

Set fso = CreateObject("Scripting.FileSystemObject")
Set fh = fso.OpenTextFile( "E:\Coding\VB\demo.txt" )
text = Trim(fh.ReadAll())
i = InStrRev(text, vbLf)
line = Mid(text, i + 1)
Slai
  • 22,144
  • 5
  • 45
  • 53
0

How about

Set fso = CreateObject("Scripting.FileSystemObject")
Set fh = fso.OpenTextFile("D:\demo.txt")
strin = Split(fh.readall, vbNewLine)
wscript.echo strin(fh.Line - 5)
brettdj
  • 54,857
  • 16
  • 114
  • 177