Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vbscript to read and write files

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use vbscript to read and write files", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use vbscript to read and write files"!

1. Open the file

Use the opentextfile method

Set fs = createobject ("scripting.filesystemobject")

Set ts=fs.opentextfile ("c:\ 1.txt", 1je true)

Note that you need to fill in the full path of the file, and the latter parameter is the access mode.

1 is forreading

2 is forwriting

8 is appending

The third parameter specifies whether to create the specified file if it does not exist.

2. Read the file

There are three ways to read a file

Read (x) reads x characters

Readline reads a row

Readall read all

For example:

Set fs = createobject ("scripting.filesystemobject")

Set ts=fs.opentextfile ("c:\ 1.txt", 1je true)

Value=ts.read (20)

Line=ts.readline

Contents=ts.readall

Here are a few more pointer variables:

The atendofstream property of the textstream object. This property returns true when it is at the end of the file. We can use loop detection without reaching the end of the file. For example:

Set fs = createobject ("scripting.filesystemobject")

Set f=fs.getfile ("c:\ 1.txt", 1Powerfalse)

Set ts=f.openastextstream (1 dint 0)

Do while ts.atendofstreamtrue

F.read (1)

Loop

There is also a property, atendofline, which returns true if the end of the line has been reached.

The Textstream object also has two useful properties, column and line.

After opening a file, both the row and column pointers are set to 1.

Look at a comprehensive example:

* * read.vbs**

Set fs = createobject ("scripting.filesystemobject")

Set f=fs.opentextfile ("c:\ 1.txt", 1je true)

Do while f.atendofstreamtrue

Data= ""

For axi1 to 5

If f.atendofstreamtrue then

Data=data+f.readline

End if

Next

Dataset=dataset+1

Wscript.echo "dataset" & dataset & ":" & data

Loop

Finally, let's talk about skipping lines in the file.

Skip (x) skips x characters

Skipline skips one line

The usage is also very simple as before, so let's not talk about it.

3. Write documents

You can write it in forwriting and forappending.

There are three methods:

Write (x)

Writeline

Writeblanklines (n) writes n blank lines

Let's look at an example:

*

Data= "hello, I like script programing"

Set fs = createobject ("scripting.filesystemobject")

If (fs.fileexists ("c:\ 2.txt")) then

Set f = fs.opentextfile ("c:\ 2.txt", 8)

F.write data

F.writeline data

F.close

Else

Set f=fs.opentextfile ("c:\ 2.txt", 2, true)

F.writeblanklines 2

F.write data

F.close

End if

Be sure to close the file after you finish writing it! In addition, if you want to read the file and write the file, you must remember to turn it off after reading so that you can open it in a written way.

Thank you for your reading, the above is the content of "how to use vbscript to read and write documents". After the study of this article, I believe you have a deeper understanding of how to use vbscript to read and write documents, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report