Q:
Hi, script expert! How do I Add rows to the top of a text file?
-- Ft
A:
Hello, ft. You know, at some point in his career, Sir Arthur Conan Doyle, he actually wrote Sherlock Holmes to death, and he thought he had done everything he could write about the detective. The public call quickly changed his mind and quickly brought Sherlock Holmes back to life. (Based on the basic plot of all soap operas .)
Our scripting experts deeply sympathize with Sir Arthur Conan Doyle. After all, we will think cyclically, "okay, come here; we have compiled everything we can write about text files ." When we had this idea, we immediately received a bunch of questions about text files, including the same question asked by three people: How to Add rows to the top of the file?
Very easy, my dear Watson (ah, my dear ft ). All you need to do is use a script similar to the following:
CopyCode The Code is as follows: const forreading = 1
Const forwriting = 2
Set objfso = Createobject ("scripting. FileSystemObject ")
Set objfile = objfso. opentextfile ("C: \ scripts \ test.txt", forreading)
Strcontents = objfile. readall
Objfile. Close
Strfirstline = "this is the new first line in the text file ."
Strnewcontents = strfirstline & vbcrlf & strcontents
Set objfile = objfso. opentextfile ("C: \ scripts \ test.txt", forwriting)
Objfile. writeline strnewcontents
Objfile. Close
Note. Interestingly, in the original novel, Xia Locke Holmes never said, "It's very simple, my dear Watson ". We don't know why he doesn't say that; he just doesn't.
Now the game has started, isn't it? We first create a pair of constants-forreading and forwriting. We will use these two constants when processing text files. Create a FileSystemObject instance and use the opentextfile method to open the file C: \ scripts \ test.txt to read the following content:
Set objfile = objfso. opentextfile ("C: \ scripts \ test.txt", forreading)
Now that the file has been opened, we use the readall method to read all the content of the file and store the content in a variable called strcontents. Then we immediately disable the file test.txt. Why? In this way, you can open the file through FileSystemObject for reading or writing, but you cannot perform these two operations at the same time. To add a new row to the top of the file, we must write the file; this means we must open it again, but this time it is for writing.
Next, we need to construct new content for the file. We cannot directly Add rows to the top of the text file; FileSystemObject only allows adding new rows to the end of the text file. Therefore, we need to create a new file in the memory, and then replace the existing test.txt content with this new file. The new file consists of the new first line, carriage return-line feed, and the existing content of the file. To construct this file, we first use the following code to store the new first line in the variable called strfirstline:
Strfirstline = "this is the new first line in the text file ."
Then we use the following code line to merge the new first line, press ENTER-line feed (Using VBScript constant vbcrlf) and the existing content of the file (stored in the variable strcontents ):
Strnewcontents = strfirstline & vbcrlf & strcontents
Now the rest of the work is to re-open test.txt (this time to write), and then use the writeline method to replace the existing content with the new file:
Set objfile = objfso. opentextfile ("C: \ scripts \ test.txt", forwriting)
Objfile. writeline strnewcontents
Then we call the close method, just like this. Okay. Close the file. Hi, we just want to imitate Sherlock Holmes.
Speaking of this, do you know Sherlock Holmes has a brother named mcloft? This is true. According to speculation, mcleft is very talented, but he is too lazy to do anything interesting or useful.
Hi, why do you say this sounds familiar? It doesn't matter; you are probably referring to another columnist who writes scripts every day.