object.OpenTextFile(filename[,iomode[,create[,format]]])
参数
object:必选项。应为FileSystemObject对象的名称。
filename:必选项。字符串表达式,指明要打开的文件名称。
iomode:可选项。输入/输出模式,是下列三个常数之一:ForReading,ForWriting,或ForAppending。
create:可选项。Boolean值,指出当指定的filename不存在时是否能够创建新文件。允许创建新文件时为True,否则为False。默认值为False。
format:可选项。三个Tristate值之一,指出以何种格式打开文件。若忽略此参数,则文件以ASCII格式打开。
设置
iomode参数可为下列设置之一:
常数值描述
ForReading1以只读模式打开文件。不能对此文件进行写操作。
ForWriting2以只写方式打开文件。不能对此文件进行读操作。
ForAppending8打开文件并在文件末尾进行写操作。
format参数可为下列设置之一:
常数值描述
TristateUseDefault-2以系统默认格式打开文件。
TristateTrue-1以Unicode格式打开文件。
TristateFalse0以ASCII格式打开文件。
说明
以下代码举例说明如何使用OpenTextFile方法打开写文件:
复制代码 代码如下:
SubOpenTextFileTest
ConstForReading=1,ForWriting=2,ForAppending=8
Dimfso,f
Setfso=CreateObject("Scripting.FileSystemObject")
Setf=fso.OpenTextFile("c:testfile.txt",ForWriting,True)
f.Write"嗨,你好!"
f.Close
EndSub
查字典教程网写的一个判断文件是否被更改过的代码
Dim fso Set fso = CreateObject("Scripting.FileSystemObject") set fn2=fso.GetFile("E:webrootjb51index2.htm") flsize2=fn2.size fldate2=fn2.datelastmodified set fn=fso.GetFile("E:webrootjb51index.htm") flsize1=fn.size fldate1=fn.datelastmodified If fso.FileExists("E:webrootjb51index2.htm") and flsize2>50000 and fldate2>fldate1 Then '判断文件的大小,如果html文件重新生成需要判断是否更新过且文件不能小于50K fso.getfile("E:webrootjb51index2.htm").copy("E:webrootjb51index.htm") if err.number=0 then WriteHistory "成功"&now()&".........","log.txt" end if '日志写入函数 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