VBS教程:fso方法-CreateTextFile 方法
CreateTextFile 方法
創(chuàng)建指定文件并返回 TextStream 對(duì)象,該對(duì)象可用于讀或?qū)憚?chuàng)建的文件。
object.CreateTextFile(filename[, overwrite[, unicode]])
參數(shù)
object
必選項(xiàng)。應(yīng)為 FileSystemObject 或 Folder 對(duì)象的名稱。
filename
必選項(xiàng)。字符串表達(dá)式,指明要?jiǎng)?chuàng)建的文件。
overwrite
可選項(xiàng)。Boolean 值指明是否可以覆蓋現(xiàn)有文件。如果可覆蓋文件,該值為 True;如果不能覆蓋文件,則該值為 False 。如果省略該值,則不能覆蓋現(xiàn)有文件。
unicode
可選項(xiàng)。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("這是一個(gè)測(cè)試。") MyFile.CloseEnd Sub
對(duì)于 filename 已經(jīng)存在的文件,如果 overwrite 參數(shù)為 False,或未提供此參數(shù)時(shí),則會(huì)出現(xiàn)錯(cuò)誤。
但很多時(shí)候?yàn)榱朔奖?,我們?huì)自定義生成文本文件的函數(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'>錯(cuò)誤號(hào):"&id&"<br>錯(cuò)誤描述:"&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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
VBS教程:對(duì)象-正則表達(dá)式(RegExp)對(duì)象
RegExp對(duì)象在VBScript中提供正則表達(dá)式支持功能,該對(duì)象有3個(gè)屬性和3個(gè)方法2006-11-11VBScript Enun Remote CMD Shell代碼
Enun Remote CMDShell,喜歡玩cmdshell的朋友可以參考下2013-07-07遠(yuǎn)程啟動(dòng)終端服務(wù)的windows腳本ROTS.vbs
遠(yuǎn)程啟動(dòng)終端服務(wù)的windows腳本ROTS.vbs...2007-02-02使用 Iisftp.vbs 暫停FTP站點(diǎn)的方法
這篇文章主要介紹了使用 Iisftp.vbs 暫停FTP站點(diǎn)的方法,需要的朋友可以參考下2014-07-07