object.OpenTextFile(filename[, iomode[, create[, format]]])
參數(shù)
object :必選項(xiàng)。應(yīng)為 FileSystemObject 對象的名稱。
filename :必選項(xiàng)。字符串表達(dá)式,指明要打開的文件名稱。
iomode :可選項(xiàng)。輸入/輸出模式,是下列三個(gè)常數(shù)之一:ForReading,F(xiàn)orWriting,或 ForAppending。
create :可選項(xiàng)。Boolean 值,指出當(dāng)指定的 filename 不存在時(shí)是否能夠創(chuàng)建新文件。允許創(chuàng)建新文件時(shí)為 True,否則為 False。默認(rèn)值為 False。
format :可選項(xiàng)。三個(gè) Tristate 值之一,指出以何種格式打開文件。若忽略此參數(shù),則文件以 ASCII 格式打開。
設(shè)置
iomode 參數(shù)可為下列設(shè)置之一:
常數(shù) 值 描述
ForReading 1 以只讀模式打開文件。不能對此文件進(jìn)行寫操作。
ForWriting 2 以只寫方式打開文件。不能對此文件進(jìn)行讀操作。
ForAppending 8 打開文件并在文件末尾進(jìn)行寫操作。
format 參數(shù)可為下列設(shè)置之一:
常數(shù) 值 描述
TristateUseDefault -2 以系統(tǒng)默認(rèn)格式打開文件。
TristateTrue -1 以 Unicode 格式打開文件。
TristateFalse 0 以 ASCII 格式打開文件。
說明
以下代碼舉例說明如何使用 OpenTextFile 方法打開寫文件:
復(fù)制代碼 代碼如下:
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 "嗨,你好!"
f.Close
End Sub
腳本之家寫的一個(gè)判斷文件是否被更改過的代碼
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
'判斷文件的大小,如果html文件重新生成需要判斷是否更新過且文件不能小于50K
fso.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.Close
End Sub
您可能感興趣的文章:- asp(vbs)fso OpenTextFile方法參數(shù)說明