VBS Tutorial: Method-openastextstream Method _vbs

Source: Internet
Author: User

OpenAsTextStream method

Opens the specified file and returns a TextStream object that is used to read, write, or append to the file.

object.OpenAsTextStream([iomode, [format]])

Parameters

Object

Required option. Should be the name of the File object.

IOMode

Options available. The input/output mode is one of the following three constants: ForReading, ForWriting, or forappending.

Format

Options available. One of three tristate values that indicates in what format the file is opened. Ignoring this parameter, the file is opened in ASCII format.

Set up

The iomode parameter can be one of the following settings:

Constants value Description
ForReading 1 Open the file in read-only mode. This file cannot be written to.
ForWriting 2 Open the file in read-write mode. If a file with the same name already exists, the old file is overwritten.
ForAppending 8 Open the file and write at the end of the file.

The format parameter can be one of the following settings:

Constants value Description
Tristateusedefault -2 Open the file in the system default format.
TristateTrue -1 Opens the file in Unicode format.
Tristatefalse 0 Opens the file in ASCII format.

Description

The OpenAsTextStream method can provide the same functionality as the OpenTextFile method of a FileSystemObject object. In addition, you can write files by using the OpenAsTextStream method.

The following code illustrates how to use the OpenAsTextStream method:

Function TextStreamTest    Const ForReading = 1, ForWriting = 2, ForAppending = 8    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0    Dim fso, f, ts    Set fso = CreateObject("Scripting.FileSystemObject")    fso.CreateTextFile "test1.txt"            'Create a file.     Set f = fso.GetFile("test1.txt")    f.OpenAsTextStream(ForWriting, TristateUseDefault)    ts.Write " Hi, Hello!"    ts.Close    f.OpenAsTextStream(ForReading, TristateUseDefault)  TextStreamTest = ts.ReadLine  ts.CloseEnd Function

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.