object.OpenTextFile(filename[, iomode[, create[, format]]])
參數(shù)
object :必選項。應為 FileSystemObject 對象的名稱。
filename :必選項。字符串表達式,指明要打開的文件名稱。
iomode :可選項。輸入/輸出模式,是下列三個常數(shù)之一:ForReading,F(xiàn)orWriting,或 ForAppending。
create :可選項。Boolean 值,指出當指定的 filename 不存在時是否能夠創(chuàng)建新文件。允許創(chuàng)建新文件時為 True,否則為 False。默認值為 False。
format :可選項。三個 Tristate 值之一,指出以何種格式打開文件。若忽略此參數(shù),則文件以 ASCII 格式打開。
設置
iomode 參數(shù)可為下列設置之一:
常數(shù) 值 描述
ForReading 1 以只讀模式打開文件。不能對此文件進行寫操作。
ForWriting 2 以只寫方式打開文件。不能對此文件進行讀操作。
ForAppending 8 打開文件并在文件末尾進行寫操作。
format 參數(shù)可為下列設置之一:
常數(shù) 值 描述
TristateUseDefault -2 以系統(tǒng)默認格式打開文件。
TristateTrue -1 以 Unicode 格式打開文件。
TristateFalse 0 以 ASCII 格式打開文件。
說明
以下代碼舉例說明如何使用 OpenTextFile 方法打開寫文件:
武林網(wǎng)寫的一個判斷文件是否被更改過的代碼
Dim fsoSet fso = CreateObject("Scripting.FileSystemObject")set fn2=fso.GetFile("E:/webroot/jb51/index2.htm")flsize2=fn2.sizefldate2=fn2.datelastmodifiedset fn=fso.GetFile("E:/webroot/jb51/index.htm")flsize1=fn.sizefldate1=fn.datelastmodifiedIf fso.FileExists("E:/webroot/jb51/index2.htm") and flsize2>50000 and fldate2>fldate1 Then'判斷文件的大小,如果html文件重新生成需要判斷是否更新過且文件不能小于50Kfso.getfile("E:/webroot/jb51/index2.htm").copy("E:/webroot/jb51/index.htm")if err.number=0 then WriteHistory "成功"&now()&".........","log.txt"end if'日志寫入函數(shù)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.CloseEnd Sub新聞熱點
疑難解答