Counting items in text file with vbs

Time:2021-3-3

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

— FT

A:
Hello, ft. You know, at one point in Sir Arthur Conan Doyle’s career, he actually killed Shylock Holmes. He thought he had finished everything he could about the great detective. The public outcry soon changed his mind, and he quickly brought Sherlock Holmes back to life. (follow the basic plot line of all soap operas.)

We script experts have great sympathy for Sir Arthur Conan Doyle. After all, we think periodically, “well, that’s it; we’ve written everything we can about the text file.” As soon as 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 lines to the top of the file?

Very simple, my dear Watson. All you need to do is use a script like this:

Copy codeThe 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 

be careful. Interestingly, in the original novel, Sherlock Holmes never said “very simple, my dear Watson”. We don’t know why he didn’t say that; he just didn’t say that.

Well, now the game has started, hasn’t it? We first create a pair of constants – forreading and forwriting, which we will use when processing text files. We create an instance of “file system object” and then use the “opentextfile” method to open the file “C:: scripts”\ Test.txt To read the following:

Set objFile = objFSO.OpenTextFile(“C:\Scripts\Test.txt”, ForReading)

Now that the file is open, we use the “read all” method to read all the contents of the file and store them in a variable called “strcontents”. Then we close the file immediately Test.txt . Why? In this way, you can open a file to read or write through the “file system object”, but you can’t do both at the same time. To add a new line to the top of the file, we will have to write to the file; that means we have to reopen it, but this time for writing.

Next, we need to construct new content for the file. We can’t add lines directly to the top of a text file; filesystem object only allows new lines to be added to the end of a text file. So what we need to do is create a new file in memory and replace the existing one with the new one Test.txt The content of. The new document will consist of three parts: a new first line; carriage return – line feed; and the existing content of the document. 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 newline (using the VBScript constant vbcrlf) and the existing contents of the file (stored in the variable ﹣ strcontents “):

strNewContents = strFirstLine & vbCrLf & strContents

Now all that’s left is to reopen Test.txt (this time for writing), and then use the “writeline” method to replace the existing content with a new file:

Set objFile = objFSO.OpenTextFile(“C:\Scripts\Test.txt”, ForWriting)
objFile.WriteLine strNewContents

Then we call the “close” method, and like this, it’s done. All right, file closed. Hi, we just want to imitate Sherlock Holmes.

Speaking of which, do you know that Sherlock Holmes has a brother named mcleft? It’s true. According to the speculation, mccleft was gifted, but he was too lazy to do anything interesting or useful.

Hi, what do you mean that sounds familiar? Never mind; you’re probably referring to some other columnist who writes scripts every day.

Recommended Today

CentOS makes document server based on nginx

CentOS makes document server based on nginx Basic environment for installation yum install gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel Find the latest stable version of nginx on the official website https://nginx.org/en/download.html Stable version: https://nginx.org/download/nginx-1.18.0.tar.gz cd /home wget -c https://nginx.org/download/nginx-1.18.0.tar.gz Decompress compressed package data tar -zxvf […]