VBS教程:fso方法-CreateTextFile 方法
CreateTextFile 方法
創(chuàng)建指定文件并返回 TextStream 對象,該對象可用于讀或?qū)憚?chuàng)建的文件。
object.CreateTextFile(filename[, overwrite[, unicode]])
參數(shù)
object
必選項。應(yīng)為 FileSystemObject 或 Folder 對象的名稱。
filename
必選項。字符串表達式,指明要創(chuàng)建的文件。
overwrite
可選項。Boolean 值指明是否可以覆蓋現(xiàn)有文件。如果可覆蓋文件,該值為 True;如果不能覆蓋文件,則該值為 False 。如果省略該值,則不能覆蓋現(xiàn)有文件。
unicode
可選項。Boolean 值指明是否以 Unicode 或 ASCII 文件格式創(chuàng)建文件。如果以 Unicode 文件格式創(chuàng)建文件,則該值為 True;如果以 ASCII 文件格式創(chuàng)建文件,則該值為 False。如果省略此部分,則假定創(chuàng)建 ASCII 文件。
說明
以下代碼舉例說明如何使用 CreateTextFile 方法創(chuàng)建并打開文本文件:
Sub CreateAfile Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("這是一個測試。") MyFile.CloseEnd Sub
對于 filename 已經(jīng)存在的文件,如果 overwrite 參數(shù)為 False,或未提供此參數(shù)時,則會出現(xiàn)錯誤。
但很多時候為了方便,我們會自定義生成文本文件的函數(shù)
Function createTextFile(Byval content,Byval fileDir,Byval code) dim fileobj,fileCode : fileDir=replace(fileDir, "\", "/") if isNul(code) then fileCode=Charset else fileCode=code call createfolder(fileDir,"filedir") if fileCode="utf-8" then on error resume next With objStream .Charset=fileCode: .Type=2: .Mode=3: .Open: .Position=0 .WriteText content: .SaveToFile Server.MapPath(fileDir), 2 .Close End With else on error resume next:err.clear set fileobj=objFso.CreateTextFile(server.mappath(fileDir),True) fileobj.Write(content) set fileobj=nothing end if if Err Then err.clear :createTextFile=false : errid=err.number:errdes=err.description:Err.Clear : echoErr err_09,errid,errdes else createTextFile=true End Function
Sub echoErr(byval str,byval id, byval des)
dim errstr,cssstr
cssstr="<meta http-equiv=""Content-Type"" content=""text/html; charset="&Charset&""" />"
cssstr=cssstr&"<style>body{text-align:center}#msg{background-color:white;border:1px solid #0073B0;margin:0 auto;width:400px;text-align:left}.msgtitle{padding:3px 3px;color:white;font-weight:700;line-height:28px;height30px;font-size:12px;border-bottom:1px solid #0073B0; text-indent:3px; background-color:#0073B0}#msgbody{font-size:12px;padding:20px 8px 30px;line-height:25px}#msgbottom{text-align:center;height:20px;line-height:20px;font-size:12px;background-color:#0073B0;color:#FFFFFF}</style>"
errstr=cssstr&"<script language=""javascript"">setTimeout(""goLastPage()"",5000);function goLastPage(){location.href='"& sitePath &"/';}</script><div id='msg'><div class='msgtitle'>提示:【"&str&"】</div><div id='msgbody'>錯誤號:"&id&"<br>錯誤描述:"&des&"<br /><a href=""javascript:history.go(-1);"">返回上一頁</a> <a href="""& sitePath &"/"">返回首頁</a></div><div id='msgbottom'>Powered by AspCms2.0</div></div>"
cssstr=""
die(errstr)
End Sub
以上就是VBS教程:fso方法-CreateTextFile 方法的詳細(xì)內(nèi)容,更多關(guān)于fso CreateTextFile的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用vbs實現(xiàn)將剪切板的unix格式的內(nèi)容處理成pc格式的代碼
用vbs實現(xiàn)將剪切板的unix格式的內(nèi)容處理成pc格式的代碼...2007-10-10
bookfind 通過ISBN序號獲取圖書連接的書名與作者的vbs代碼
bookfind 通過ISBN序號獲取圖書連接的書名與作者的vbs代碼,類似小偷程序,通過正則匹配,雖然現(xiàn)在已經(jīng)無法使用,但代碼不錯,原理都有2011-12-12

