写入文件失败 - VBScript

时间:2015-06-11 17:45:49

标签: vbscript

我无法让此脚本在Windows 8上运行。它适用于Windows 7及更低版本。当我运行它时,它告诉我写入文件失败,第32行char 7(行首)。目标是从我们的服务器中提取一个zip文件并将其放在ExtractTo中所述的文件夹中。

Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")

Dim objShell
Dim Documents

Set objShell = CreateObject("Shell.Application")
Set Documents = objShell.Namespace("MyDocuments")

strFileURL = "http://www.markettraders.com/downloads/UCS-Indicators/Active-Scalper/Active-Scalper.zip"

strHDLocation = Documents & "\Ultimate Charting\Custom indicators\Active-Scalper.zip"


Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, fal
objXMLHTTP.send()

If objXMLHTTP.Status = 200 Then
    Set objADOStream = CreateObject("ADODB.Stream")
    objADOStream.Open
    objADOStream.Type = 1 'adTypeBinary

    objADOStream.Write objXMLHTTP.ResponseBody
    objADOStream.Position = 0

    objADOStream.SaveToFile strHDLocation
    objADOStream.Close
    Set objADOStream = Nothing
End if

Set objXMLHTTP = Nothing

ZipFile= Documents & "\Ultimate Charting\Custom indicators\Active-Scalper.zip"

ExtractTo = Documents &  "\Ultimate Charting\Custom indicators\"

Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(ExtractTo) Then
    fso.CreateFolder(ExtractTo)
End If

set objShell = CreateObject("Shell.Application")
set FilesInZip=objShell.NameSpace(ZipFile).items
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
Set fso = Nothing
Set objShell = Nothing

set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(ZipFile) Then
    fso.DeleteFile(ZipFile)
End If

WScript.Echo "Your custom indicator has been added!"
Wscript.quit

1 个答案:

答案 0 :(得分:-1)

您可能在准备好之前尝试阅读响应。您需要检查readyState属性以查看请求是否已完成(状态= 4)。

If objXMLHTTP.Status = 200 And objXMLHTTP.readyState = 4 Then

但是你在一行上发生了两件大事,所以为了帮助你自己调试你的问题,请稍微分开你的陈述并防范明显的异常:

s = objXMLHTTP.ResponseBody

If Len(s) > 0 Then
    objADOStream.Write s
End If

然后重新运行脚本并查看错误发生的位置。