VBScript » FileSystemObject » OpenTextFile

Version: 2.0

Syntax:
object.OpenTextFile (filename [, iomode[, create[, format]]])
iomode
The optional iomode argument can have one of the values from the iomode Constants table as its value.
create
The optional create argument can be either True, which will create the specified file if it does not exist, or False, which won't.
format
The optional format argument uses one of values from the Tristate Constants table to specify in which format the file is opened. If not set, this defaults to TristateFalse, and the file will be opened in ASCII format.

Opens the file specified in the filename parameter and returns an instance of the TextStreamObject for that file. )

This method is used to open a text file and returns a TextStreamObject that can then be used to write to, append to, and read from the file.

iomode Constants

CONSTANT VALUE DESCRIPTION
ForReading 1 Opens a file for reading only
ForWriting 2 Opens a file for writing. If the file already exists, the contents are overwritten.
ForAppending 8 Opens a file and starts writing at the end (appends). Contents are not overwritten.


Tristate Constants

CONSTANT VALUE DESCRIPTION
TristateTrue -1 Opens the file as Unicode
TristateFalse 0 Opens the file as ASCII
TristateUseDefault -2 Use default system setting

Examples

Code:
<%
dim filesys, filetxt
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("c:\somefile.txt", ForAppending, True)
filetxt.WriteLine("Your text goes here.")
filetxt.Close
%>
Explanation:

This example will open the file, "c:\somefile.txt" (or create it if it does not exist), and append the specified text to it.