Read the last line of a text file with the VBS _VBS

Source: Internet
Author: User
Tags constant readline
Ask:
Hello, Scripting Guy! How do I read only the last line of a text file?
--BM
For:
Hello, BM. If you ever want to know Hello, Scripting Guys! Unlike other daily columns (such as intimate sister), here's a way to do it. Suppose someone in the letter to the bosom sister said:
Bosom Sister:
My life is a mess, and I need to take some steps to deal with it. How can I turn my life around and be happy again?
Desperate people in Dayton
Bosom sister would never give such a reply to a desperate man:
Dear Desperate man:
Alas, your life cannot be better. I'm so sorry.
So, how do you think the Scripting Guys will respond to your last line about reading a text file? That's correct:
Dear BM:
Alas, you cannot. I'm so sorry.
However, please wait a moment, do not go before. Indeed, the bosom sister would never say, "You know, desperate, there's nothing I can do to make you happy." But there is a way to make you look happy. "Yet the Scripting Guys have no such qualms. In other words, we cannot provide you with a script that reads only the last line of a text file. But the following script can appear to read only the last line of a text file:
Copy Code code as follows:

Const ForReading = 1
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("C:\Scripts\Test.txt", ForReading)
Do Until Objfile.atendofstream
StrLine = Objfile.readline
Loop
Objfile.close
WScript.Echo StrLine

The problem we're experiencing here is that FileSystemObject (the scripting object for working with text files) knows only one Direction: forward. It must start at the beginning of the file and can only continue to run to the end of the file. You cannot specify a different starting position and cannot read backwards (that is, from the end to the beginning). In fact, you can't even reread the file: You can't reread the file until the end of the file until you close it and reopen it. That's why the tutorial's solution is almost identical when it comes to text files.
The workaround here is that we actually do read the entire text file from beginning to the beginning. However, we only track the last line that was read. When we read the end of the file, we get a variable that contains the value of the last row read, and it is also the last line of the file. When you echo the value of the variable, it looks as if we read only the last line (especially when there is nothing else to process, the FileSystemObject is very fast). We didn't-we actually read the whole file-but no one would know. It's going to be our little secret.
As for the code itself, we first define the constant named ForReading and set its value to 1, and we'll use that constant to tell FileSystemObject that we want to open the text file to be read. Then, we create an instance of Scripting.FileSystemObject and use the OpenTextFile method to open the file C:\Scripts\Test.txt. Next is the following code block:
Copy Code code as follows:

Do Until Objfile.atendofstream
StrLine = Objfile.readline
Loop

All we have to do here is read the file line-by-row until the end of the file (that is, the end of the file stream). Each time we read a row, we replace the value of the variable strLine with the text we just read. For example, suppose a text file contains three lines:
A

C
Read row 1 in our loop, so assign value A to StrLine. Reads the second row in the next loop, which means that the value B is assigned to StrLine. Loop again and assign the value C to StrLine. Because we have reached the end of the file, StrLine holds the value C, which is exactly the last line of the file. Then close the file and echo the StrLine value. All people know only what we do is read-return-the value of the last line of the file.
Yes, it's very secretive.
There is no denying that there is a potential problem with this script. Suppose a few lines of blank lines are added to the end of the file. The script will faithfully return empty (null value) as the last line of the text file. This is what it should do: After all, the last line of the file is blank. But suppose this is some type of log file, and for some reason the application that created the log always places blank lines at the end of the file. In this case, you may be really interested in the last NonBlank line in the file. Here is the modified script, which uses the Len function to check the length of each row read. If the length equals 0, this means that the row is blank and does not store the value in the variable strLine:
Copy Code code as follows:

Const ForReading = 1
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("C:\Scripts\Test.txt", ForReading)
Do Until Objfile.atendofstream
Strnextline = Objfile.readline
If Len (Strnextline) > 0 Then
StrLine = Strnextline
End If
Loop
Objfile.close
WScript.Echo StrLine
Envy, bosom sister!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.