摘自MSDNCreateTextFile Method Language Reference Version 2 See Also Applies To --------------------------------------------------------------------------------Description Creates a specified file name and returns a TextStream object that can be used to read from or write to the file. Syntax object.CreateTextFile(filename[, overwrite[, unicode]]) The CreateTextFile method has these parts:Part Description object Required. Always the name of a FileSystemObject or Folder object. filename Required. String expression that identifies the file to create. overwrite Optional. Boolean value that indicates whether you can overwrite an existing file. The value is true if the file can be overwritten, false if it can't be overwritten. If omitted, existing files are not overwritten. unicode Optional. Boolean value that indicates whether the file is created as a Unicode or ASCII file. The value is true if the file is created as a Unicode file, false if it's created as an ASCII file. If omitted, an ASCII file is assumed. Res The following code illustrates how to use the CreateTextFile method to create and open a text file: var fso = new ActiveXObject("Scripting.FileSystemObject"); var a = fso.CreateTextFile("c:\\testfile.txt", true); a.WriteLine("This is a test."); a.Close();
strpath="c:\ddd\test.txt"set fso = createobject("Scripting.FileSystemObject") if fso.folderexists(left(strpath,instrrev(strpath,"\"))) then set a = fso.CreateTextFile(strpath, true) a.WriteLine("xxx") a.Close() else msgbox "路径不存在!",vbexclamation end if
运行结果你已经知道了,路径不存在就是报错退出,组件就是这样设计的你有什么办法,要么就在它的下一行检测下错误代码。strpath="c:\ddd\test.txt"on error resume next set fso = createobject("Scripting.FileSystemObject") set a = fso.CreateTextFile(strpath, true)if err.number>0 then msgbox "错误号: " & err.number & vbcrlf & "错误描述: " & err.description end ifa.WriteLine("xxx") a.Close()
<H3> Thank you for submitting your information </H3> <% '----------------------------------------------------- ' FileSystemObject constants include file for VBScript '----------------------------------------------------- '---- iomode Values ---- Const ForAppending = 8 Const ForReading = 1 Const ForWriting = 2 '---- format Values ---- Const TristateFalse = 0 Const TristateMixed = -2 Const TristateTrue = -1 Const TristateUseDefault = -2 ' If the file already exists, then an error will be generated. ' This line permits the program to continue executing on error. ' Resume next. ' Create the scripting object. set objfso = createobject("scripting.FileSystemObject") ' Create the text file. If the file already exists, then an error ' will be generated. set myobject = objfso.createtextfile("c:\public\myfile.txt", false) ' Open the file for scripting. set myobject = objfso.opentextfile("c:\public\myfile.txt", forappending ) 'Calculate the total price. quantity = request.form("quantity") price = request.form("price") totalprice = quantity * price ' Assign the submitted form results to a variable. productdata = request.form("name") & "," & request.form("product") & "," & request.form("quantity") & "," & request.form("price") & "," & totalprice ' Write the form results to the file. myobject.writeline(productdata) ' Close the object. myobject.close %>
Set fs = CreateObject("Scripting.FileSystemObject") Set aFile = fs.CreateTextFile("c:\SiteAccessFile.txt", True) aFile.WriteLine("You just wrote to a text file!") aFile.Close
主题是这个方法执行失败返回值,那个asp代码只是写入文件范例,没涉及写入失败错误捕获什么的类似于1楼的例子: var fso = new ActiveXObject("Scripting.FileSystemObject"); var a = fso.CreateTextFile("c:\\testfile.txt", true); a.WriteLine("This is a test."); a.Close();
Version 2
See Also Applies To
--------------------------------------------------------------------------------Description
Creates a specified file name and returns a TextStream object that can be used to read from or write to the file.
Syntax
object.CreateTextFile(filename[, overwrite[, unicode]])
The CreateTextFile method has these parts:Part Description
object Required. Always the name of a FileSystemObject or Folder object.
filename Required. String expression that identifies the file to create.
overwrite Optional. Boolean value that indicates whether you can overwrite an existing file. The value is true if the file can be overwritten, false if it can't be overwritten. If omitted, existing files are not overwritten.
unicode Optional. Boolean value that indicates whether the file is created as a Unicode or ASCII file. The value is true if the file is created as a Unicode file, false if it's created as an ASCII file. If omitted, an ASCII file is assumed.
Res
The following code illustrates how to use the CreateTextFile method to create and open a text file:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
if fso.folderexists(left(strpath,instrrev(strpath,"\"))) then
set a = fso.CreateTextFile(strpath, true)
a.WriteLine("xxx")
a.Close()
else
msgbox "路径不存在!",vbexclamation
end if
set fso = createobject("Scripting.FileSystemObject")
set a = fso.CreateTextFile(strpath, true)if err.number>0 then
msgbox "错误号: " & err.number & vbcrlf & "错误描述: " & err.description
end ifa.WriteLine("xxx")
a.Close()
<%
'-----------------------------------------------------
' FileSystemObject constants include file for VBScript
'----------------------------------------------------- '---- iomode Values ----
Const ForAppending = 8
Const ForReading = 1
Const ForWriting = 2 '---- format Values ----
Const TristateFalse = 0
Const TristateMixed = -2
Const TristateTrue = -1
Const TristateUseDefault = -2 ' If the file already exists, then an error will be generated.
' This line permits the program to continue executing on error.
' Resume next.
' Create the scripting object.
set objfso = createobject("scripting.FileSystemObject") ' Create the text file. If the file already exists, then an error
' will be generated.
set myobject = objfso.createtextfile("c:\public\myfile.txt", false) ' Open the file for scripting.
set myobject = objfso.opentextfile("c:\public\myfile.txt", forappending ) 'Calculate the total price.
quantity = request.form("quantity")
price = request.form("price")
totalprice = quantity * price ' Assign the submitted form results to a variable.
productdata = request.form("name") & "," & request.form("product") & "," &
request.form("quantity") & "," & request.form("price") & "," & totalprice ' Write the form results to the file.
myobject.writeline(productdata) ' Close the object.
myobject.close
%>
Set aFile = fs.CreateTextFile("c:\SiteAccessFile.txt", True)
aFile.WriteLine("You just wrote to a text file!")
aFile.Close
这个是asp代码,功能是将表单传递过来的数据保存到本地的文本文件中。和主题貌似无关。
应该判断的是错误号不是其返回值
ASP代码不是VBS写的?
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
#10楼 已经回答了