Hi Dw.

I have a very huge text file with size "662 MB" and its can't be opened by a notepad on my computer so I created a program to only read the last line because there is a software that is writing to this file some counts, so I don't want the counts to be repeated so if the computer happens to shutdown or crash, I use this program to read only the last line of this file and populate the text on the last line.

Now the problem is that I don't know what happened but a battery was drained and the laptop shutdown and someone tried to turn it on but that coursed the computer to dump everything because the battery was already low. Now the problem is that the program the time the computer was started while the battery was drained the program only wrote a blank on a text file so the last line is blank.

How can I read the secondlast, thirdlast line on a text file?

Here is the code I use to read only the last line.

 Using sr As New StreamReader(File.OpenRead("D:\test"))
 Dim LastLine As String, I, J As Integer, P As Long
 Do
     I = 0 : J += 128 : P = sr.BaseStream.Length - J
     If P < 0 Then P = 0
     sr.BaseStream.Position = P
     Do
         LastLine = sr.ReadLine : I +=1
     Loop Until  sr.EndOfStream
 Loop Until I > 1 Or P = o
 sr.Close()
 lblDisplay.Text = LastLine
 End Using

Thank you.

Why not write a one-shot program that reads the file line-by-line and writes a new file, skipping any blank lines? Replace your old file with the new file. Then you can go back to using your original code.

You may want to consider partitioning the text file into smaller, more manageable pieces.

Alternatively reading the next line into a temporary variable will allow you to weed out any blank lines and only keep the last populated line:

 Using sr As New StreamReader(File.OpenRead("D:\test"))
 Dim LastLine As String, I, J As Integer, P As Long
 Do
     I = 0
     J += 128 
     P = sr.BaseStream.Length - J
     If P < 0 Then P = 0
     sr.BaseStream.Position = P
     Do
         Dim nextLine As String = sr.ReadLine
         If nextLine <> "" Then
             LastLine =  nextLine
             I +=1
         End If
     Loop Until  sr.EndOfStream
 Loop Until I > 1 Or P = o
 sr.Close()
 lblDisplay.Text = LastLine
 End Using

@Jim, let's me just enlighten you what I'm doing. I have program1 which performs a count and for each count its writes it on the file the format is "00000-00000" its started like this: 00000-00001, 00000-00002, 00000-00003, 00000-00004, 00000-00005 now its around 00520-98164 its will stop at 99999-99999

I haven't added the code to read the last line and extract each digit so that the count will continue because I will have to extract each digit to its own variable for easy of counting.
So for now I use the program2 to get the last written count and populate it on the label then I type that value on program1 code (variables)

@Tinstaafl thanks, will try out that and see.

Just tried the above but still the label is blank. I think if I could be able to remove the blanks I will end up with the last known count. Is there a way to remove the blanks at the end of text file without reading the entire file because the file is large, its can't be opened now on my computer my RAM has low memory.

Are there any blank lines other than the last line? If I wrote a script to remove all blank lines would that damage the file?

@Jim. Normally there are no blanks because every count is written on a new line but the file I think got some few blanks at the end of which I also don't want them because it will produce problems such as this I'm having and also when I want to get the total lines are there in a file. So I don't think a script will damage course I don't want the blanks on a file.

Since the code I gave you doesn't do what you want, I would suspect that the bad lines aren't completely blank and there's something on the line, possibly a space or some control character. It'll be hard to weed those lines out without knowing exactly what is in those lines. Using Debug to examine the string returned by those lines would help.

How?

Copy the following code into a file named StripBlanks.vbs

set fso = CreateObject("Scripting.FileSystemObject")
set stdin = Wscript.StdIn
set stdout = Wscript.StdOut

do while not stdin.AtEndOfStream
    line = Trim(stdin.ReadLine)
    if not line = "" then
        stdout.WriteLine(line)
    end if
loop

If your original file is named myfile.txt then you can create a stripped (no empty lines) version of that file by opening a command shell and typing

StripBlanks <myfile.txt >mynewfile.txt

assuming that StripBlanks.vbs is in the same folder as the text file or in a folder in %PATH%.

Set a BreakPoint on the line just after Readline is called. When the execution stops examine the value stored in nextline. If it's a full line hit the Continue button on the toolbar. Keep doing this until you get a line that isn't full, and see what value(s) it holds.

@Jim the script when I run produce this error:

Script: C:\Windows\system32\StripBlanks.vbs
Line: 5
Char: 1
Error: The handling is invalid
Code: 80070006
Source: (null)

Any reason why I'm seeing this error when trying to run your script?

Thanks.

There are actually two scripting engines. Wscript.exe is the "windowsd" version and Cscript.exe is the "command line" version. I believe you can fix the problem either by

cscript StripBlanks.vbs <myfile.txt >mynewfile.txt

or you can set cscript as the default by

cscript //h:cscript //save

and then you can use the original example as

StripBlanks <myfile.txt >mynewfile.txt

Wscript does not support StdIn and StdOut. Here's a link to the scripting help file which is not included on your system by default.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.