Object. OpenTextFile (filename[, iomode[, create[, format]])
Parameters
Object: Required option. Should be the name of the FileSystemObject object.
FileName: Required option. A string expression that indicates the name of the file to open.
IOMode: Options available. The input/output mode is one of the following three constants: Forreading,forwriting, or ForAppending.
Create: Optional. Boolean value that indicates whether a new file can be created when the specified filename does not exist. True when new files are allowed to be created, otherwise False. The default value is False.
Format: Options available. One of three tristate values that indicates in what format the file is opened. If this argument is omitted, the file is opened in ASCII format.
Set up
the IOMode parameter can be one of the following settings:
Constant numerical description
ForReading 1 Opens the file in read-only mode. This file cannot be written to.
ForWriting 2 Opens the file in write-only mode. This file cannot be read.
ForAppending 8 Opens the file and writes at the end of the file.
The format parameter can be one of the following settings:
Constant numerical description
TristateUseDefault-2 opens 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 following code illustrates how to open a write file using the OpenTextFile method:
Copy Code code as follows:
Sub Opentextfiletest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim FSO, F
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f = fso. OpenTextFile ("C:\testfile.txt", ForWriting, True)
F.write "Hi, Hello! "
F.close
End Sub
A code that determines whether a file has been changed by a cloud-dwelling community
Dim FSO
Set fso = CreateObject ("Scripting.FileSystemObject")
set FN2=FSO. GetFile ("E:\webroot\jb51\index2.htm")
flsize2=fn2.size
fldate2=fn2.datelastmodified
set FN=FSO. GetFile ("E:\webroot\jb51\index.htm")
flsize1=fn.size
fldate1=fn.datelastmodified
If fso. FileExists ("E:\webroot\jb51\index2.htm") and flsize2>50000 and fldate2>fldate1 Then
' Determine the size of the file, If the HTML file regeneration needs to be judged whether it has been updated and the file cannot be less than 50K
fso.getfile ("E:\webroot\jb51\index2.htm"). Copy ("E:\webroot\jb51\index.htm ")
if Err.number=0 then WriteHistory" Success "&now () &" ... "," Log.txt "End
if"
log write function
Sub writehistory (hischars, path)
Const ForReading = 1, ForAppending = 8
Dim fso, f
Set fso = CreateObject (" Scripting.FileSystemObject ")
Set f = fso. OpenTextFile (Path, ForAppending, True)
f.writeline hischars
f.close End
Sub