Use vbs to read the last line of a text file

Source: Internet
Author: User

Q:
Hello, script expert! How can I read only the last line of a text file?
-- BM
A:
Hello, BM. If you ever want to know Hello, script expert! Different from other daily columns (for example, Zhixin sister), the following is a way. Suppose someone says in a letter to Zhixin's sister:
Zhixin sister:
My life is a mess and I need to take some measures to deal with it. How can I change my life and be happy again?
Desperate man in Dayton
Zhixin will never give such a reply to a desperate person:
Dear Desperate person:
Alas, your life cannot be better. Sorry.
How do you think the script experts reply to your last line about reading text files? Correct:
Dear BM:
Alas, you cannot. Sorry.
However, please wait. Indeed, Zhixin never said, "You know, Desperate, I can't do anything to make you happy. But there is a way to make you look happy ." However, script experts have no such doubts. In other words, we cannot provide you with scripts that read only the last line of the text file. However, the following script can read only the last line of the text file: Copy codeThe Code is 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 encountered here is that FileSystemObject (the script object used to process text files) only knows one direction: forward. It must start with the beginning of the file and can only continue to run at the end of the file. You cannot specify other starting positions and cannot reverse read (from the end to the beginning ). In fact, you cannot even re-read the file: you cannot re-read the file after it reaches the end of the file unless it is closed and then re-opened. This is why the solution to the tutorial is almost the same when it comes to text files.
The solution here is that we actually read the entire text file from the beginning to the end. However, we only track the last row read. When we read the end of the file, we will get a variable that contains the value of the last row read. It is also the last row of the file. When the value of this variable is displayed, it seems that we only read the last row (especially when there is no other thing to process, FileSystemObject is very fast ). We didn't-we actually read the entire file-but no one would know. This is our secret.
As for the Code itself, we first define a constant named ForReading and set its value to 1. We will use this 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 codeThe Code is as follows: Do Until objFile. AtEndOfStream
StrLine = objFile. ReadLine
Loop

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

C
Read Row 1 in our loop, so value A is assigned to strLine. Read the second row in the next loop, which means that value B is assigned to strLine. Recycle once and assign value C to strLine. Because we have reached the end of the file, strLine maintains the value C, which is exactly the last row of the file. Close the file and display the strLine value. All people know that what we do is read-return-the value of the last row of the file.
Yes, it is very confidential.
It is undeniable that this script has a potential problem. Assume that several lines of blank lines are added at the end of the file. The script faithfully returns NULL 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 for some reason, the application that creates this log always places blank lines at the end of the file. In this case, you may be interested in the last non-blank row in the file. The following is the modified script, which uses the Len function to check the length of each row read. If the length is equal to 0, it means that the row is blank and the value is not stored in the variable strLine:Copy codeThe Code is 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, Zhixin!

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.