METHOD: FileSystemObject.OpenTextFile
Implemented in version 2.0
object.OpenTextFile (filename [, iomode[, create[, format]]])
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.
The optional iomode argument can have one of the following Constants as its value:
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. |
The optional create argument can be either True, which will create the specified file if it does not exist, or False, which won’t.
The optional format argument uses one of the following Tristate values 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.
CONSTANT |
VALUE |
DESCRIPTION |
TristateTrue |
-1 |
Opens the file as Unicode |
TristateFalse |
0 |
Opens the file as ASCII |
TristateUseDefault |
-2 |
Use default system setting |
The following example will open the file, "c:somefile.txt" (or create it if it does not exist), and append the specified text to it.
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
%>
Source: DevGuru VBScript Method: FileSystemObject.OpenTextFile