OpenTextFile is a method in the asp language. It opens the specified file and returns a TextStream object. The file can be read, written or appended through this object.
OpenTextFile is a method in asp language
Opens the specified file and returns a TextStream object through which the file can be read, written, or appended.
object.OpenTextFile(filename[, iomode[, create[, format]]])
1. The method editor opens the specified file and returns a TextStream object, through which the file can be read, written, or appended.
object.OpenTextFile(filename[, iomode[, create[, format]]])
2. Parameter editing object
Required. object should be the name of a FileSystemObject.
filename
Required. A string expression that specifies the file to open.
iomode
Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create
Optional. Boolean value indicating whether to create a new file when the specified filename does not exist. The value is True if a new file is created, False if it is not created. If omitted, no new file is created.
format
Optional. Use one of the three-state values to specify the format in which to open the file. If omitted, the file will be opened in ASCII format.
3. Set and edit the iomode parameter, which can be any of the following settings:
Constant value description
ForReading 1 opens the file read-only. This file cannot be written.
ForWriting 2 Open file for writing
ForAppending 8 opens the file and starts writing from the end of the file.
The format parameter can be any of the following settings:
value description
TristateTrue opens files in Unicode format.
TristateFalse opens the file in ASCII format.
TristateUseDefault Opens the file using system defaults.
4. Instructions Edit The following code illustrates how to use the OpenTextFile method to open a file and append text:
var fs, a, ForAppending;
ForAppending = 8;
fs = new ActiveXObject(Scripting.FileSystemObject);
//Can be one of three constants: ForReading, ForWriting or ForAppending
//They are 1, 2, 8 respectively
a = fs.OpenTextFile(c://testfile.txt, 2, false);
...
a.Close();
Use cases in vbs script
Opens the specified file and returns a TextStream object that can be read, written, or appended to the file.
object.OpenTextFile(filename[,iomode[,create[,format]]])
parameter
object
Required. Should be the name of a FileSystemObject object.
filename
Required. A string expression specifying the name of the file to open.
iomode
Optional. Input/output mode, one of the following three constants: ForReading, ForWriting, or ForAppending.
create
Optional. Boolean value indicating whether a new file can be created when the specified filename does not exist. True to allow creation of new files, False otherwise. The default value is False.
format
Optional. One of three Tristate values indicating the format in which to open the file. If this parameter is omitted, the file is opened in ASCII format.
The iomode parameter can be one of the following settings:
constant | value | describe |
ForReading | 1 | Open the file in read-only mode. This file cannot be written. |
ForWriting | 2 | Open the file for writing only. This file cannot be read. |
ForAppending | 8 | Open the file and write at the end of the file. |
The format parameter can be one of the following settings:
constant | value | describe |
TristateUseDefault | -2 | Open the file in the system default format. |
TristateTrue | -1 | Open the file in Unicode format. |
TristateFalse | 0 | Open the file in ASCII format. |
Usage examples:
- SubOpenTextFileTest
- ConstForReading=1,ForWriting=2,ForAppending=8
- Dimfso,f
- Setfso=CreateObject(Scripting.FileSystemObject)
- Setf=fso.OpenTextFile(c:/testfile.txt,ForWriting,True)
- f.Write Hi there!
- f.Close
- EndSub
- CallOpenTextFileTest
A function written by Wulin.com
- Dimfso
- Setfso=CreateObject(Scripting.FileSystemObject)
- setfn2=fso.GetFile(E:/webroot/vevb/index2.htm)
- flsize2=fn2.size
- fldate2=fn2.datelastmodified
- setfn=fso.GetFile(E:/webroot/vevb/index.htm)
- flsize1=fn.size
- fldate1=fn.datelastmodified
- Iffso.FileExists(E:/webroot/vevb/index2.htm)andflsize2>50000andfldate2>fldate1Then
- 'Determine the size of the file. If the html file is regenerated, you need to determine whether it has been updated and the file cannot be less than 50K
- fso.getfile(E:/webroot/vevb/index2.htm).copy(E:/webroot/vevb/index.htm)
- iferr.number=0thenWriteHistory successful&now()&..........,log.txt
- endif
- 'Log writing function
- SubWriteHistory(hisChars,path)
- ConstForReading=1,ForAppending=8
- Dimfso,f
- Setfso=CreateObject(Scripting.FileSystemObject)
- Setf=fso.OpenTextFile(path,ForAppending,True)
- f.WriteLinehisChars
- f.Close
- EndSub