SoFunction
Updated on 2025-04-09

Use vbs to count items in text files

ask:
Hi, script expert! How to add lines to the top of a text file?

-- FT

answer:
Hello, FT. You know, at some point in Sir Arthur Conan Doyle's career, he actually wrote Sherlock Holmes to death, and he thought he had finished everything about this great detective could write. The public yelled quickly changed his mind, and he quickly brought Sherlock Holmes back to life. (Create it all the way according to the basic plot of all soap operas.)

Our script experts have deep sympathy for Sir Arthur Conan Doyle. After all, we would think periodically, "Okay, let's get to this; we've written everything we can write about text files." As soon as we had this idea, we immediately received a bunch of questions about text files, including the same question three people asked: How to add lines to the top of the file file?

It's very simple, my dear Watson (ah, my dear FT). All you need to do is use a script like the following:

Copy the codeThe code is as follows:

Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("") 
Set objFile = ("C:\Scripts\", ForReading) 

strContents =  
 

strFirstLine = "This is the new first line in the text file." 
strNewContents = strFirstLine & vbCrLf & strContents 

Set objFile = ("C:\Scripts\", ForWriting) 
 strNewContents 

 
Notice. It is interesting that in the original novel Sherlock Holmes never said "It's simple, my dear Watson." We don't know why he doesn't say that; he just doesn't say that.


OK, now the game has begun, isn’t it? We first create a pair of constants - ForReading and ForWriting, which we will use when processing text files. We create a FileSystemObject instance, and then open the file C:\Scripts\  to read the following content:

Set objFile = ("C:\Scripts\", ForReading)

Now that the file is open, we use the ReadAll method to read the entire content of the file and store the content in a variable called strContents. Then we close the file immediately. Why? In this way, FileSystemObject can be used to open files for reading or writing, but these two operations cannot be performed at the same time. To add a new line to the top of the file, we will have to write to the file; this means we have to reopen it, but this time for writing.

Next we need to construct new content for the file. We cannot directly add lines to the top of the text file; FileSystemObject only allows adding new lines to the end of the text file. So what we need to do is create a brand new file in memory and then replace the existing content with this new file. The new file will consist of three parts: the new first line; the carriage return-line wrap; and the existing contents of the file. To construct this file, we first use the following code to store the new first line in a variable called strFirstLine:

strFirstLine = "This is the new first line in the text file."

Then we use the following line of code to merge the new first line, carriage return-line wrap (using the 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 reopen (this time for writing), and then use the WriteLine method to replace the existing content with the new file:

Set objFile = ("C:\Scripts\", ForWriting)
 strNewContents

Then we call the Close method, and like this, the job is done. OK, the file is closed. Hi, we just want to imitate Sherlock Holmes.

Speaking of this, did you know that Sherlock Holmes had an older brother named McCroft? This is true. It is speculated that McCroft is very talented, but he has not done anything interesting or useful because he is too lazy.

Hi, what do you mean by saying this sounds familiar? That's OK; you're probably referring to some other columnist who writes scripts every day.