【问题标题】:Using FileSystemObject to Export VBScript Code to Text File使用 FileSystemObject 将 VBScript 代码导出到文本文件
【发布时间】:2014-12-05 20:01:38
【问题描述】:

我希望将以下代码的MsgBox oXMLHttp.responseText 部分替换为将结果导出到文本文件的代码。我检查了this post 并找到了以下代码:

设置 objFSO=CreateObject("Scripting.FileSystemObject")

' How to write file
outFile="c:\test\autorun.inf"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test string" & vbCrLf
objFile.Close

但是,我不知道如何实际实现这一点。 outFile 是要创建的文件的位置和名称吗?我相信是的。但是,objFile.Write "test string" & vbCrLf 的目的是什么?我的主要问题是:我的意思是告诉创建的 FileSystemObject 根据下面的代码进行处理?

Dim request, oXMLHttp, url
    url = "http://ws.cdyne.com/phoneverify/phoneverify.asmx"

    request = "<?xml version='1.0' encoding='utf-8'?>" & _
    "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
    "<soap:Body>" & _
    "<CheckPhoneNumbers xmlns=""http://ws.cdyne.com/PhoneVerify/query"">" & _
    "<PhoneNumbers>" & _
    "<string >1</string>" & _
    "<string >2</string>" & _
    "<string >3</string>" & _
    "<string >4</string>" & _
    "<string >5</string>" & _
    "<string >6</string>" & _
    "<string >7</string>" & _
    "<string >8</string>" & _
    "<string >9</string>" & _
    "<string >10</string>" & _
    "</PhoneNumbers>" & _ 
    "<LicenseKey>Key</LicenseKey>" & _
    "</CheckPhoneNumbers>" & _
    "</soap:Body>" & _
    "</soap:Envelope>"

    Set oXMLHttp = CreateObject("MSXML2.ServerXMLHTTP")
    oXMLHttp.open "POST", url, False
    oXMLHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    oXMLHttp.send request
    response = oXMLHttp.responseText

    MsgBox oXMLHttp.responseText

【问题讨论】:

    标签: vb.net vbscript filesystemobject


    【解决方案1】:
    1. 创建FileSystemObject

      Set objFSO = CreateObject("Scripting.FileSystemObject")
      
    2. 用它来创建一个TextStream 对象,代表您要创建的文件(参见here 了解CreateTextFile 函数):

      Set objFile = objFSO.CreateTextFile("c:\yourfile.txt", True)
      
    3. 使用 TextStream 对象 (objFile) 将文本写入新文件:

      objFile.Write "some text"
      

      在您的情况下,您似乎想要编写 HTTP 响应:

      objFile.Write oXMLHttp.responseText
      
    4. 关闭你的文件:

      objFile.Close
      

    【讨论】:

    • 效果很好,感谢您花时间分解代码的工作原理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2010-12-18
    • 2011-03-08
    • 2020-02-13
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多