【问题标题】:Download file via vbs通过vbs下载文件
【发布时间】:2018-07-17 01:17:52
【问题描述】:

我知道这个问题已经被问到并得到了回答,这是我找到开始我的项目的代码的地方。但它不起作用。

我被困住了。我已经尝试过,但代码不起作用。

Dim xHttp: Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim bStrm: Set bStrm = CreateObject("Adodb.Stream")
xHttp.Open "GET", "https://filexxx.exe", False
xHttp.Send

With bStrm
    .Type = 1 'binary
    .Open
    .Write xHttp.ResponseBody 'this part removed, error, tools not yet avail
    .SaveToFile "c:\temp\xxx.exe", 2 'overwrite
End With

还有这个。

strFileURL = "https://filexxx.exe"
strHDLocation = "C:\temp\xxx.exe"

Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")

objXMLHTTP.Open "GET", strFileURL, False
objXMLHTTP.Send()

Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary

objADOStream.SaveToFile strHDLocation

还有这个。

strFileURL = "https://filexxx.exe"
strHDLocation = "C:\temp\filexxx.exe"
proxy = null
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Set wshShell = CreateObject( "WScript.Shell" )
Set objUserVariables = wshShell.Environment("USER")

'http proxy is optional
'attempt to read from HTTP_PROXY env var first
On Error Resume Next

If Not (objUserVariables("HTTP_PROXY") = "") Then
    proxy = objUserVariables("HTTP_PROXY")
ElseIf Not (WScript.Arguments.Named("proxy") = "") Then
    proxy = WScript.Arguments.Named("proxy")
End If

If Not isNull(proxy) Then
    Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    objXMLHTTP.SetProxy 2, proxy
End If

On Error Goto 0

objXMLHTTP.Open "GET", strFileURL, False
objXMLHTTP.Send()
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Position = 0
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(path) Then objFSO.DeleteFile path
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
Set objXMLHTTP = Nothing

还有这个

HTTPDownload "https:filexxx.exe", "C:\temp\filexxx.exe"

Sub HTTPDownload(myURL, myPath)
    ' Standard housekeeping
    Dim i, objFile, objFSO, objHTTP, strFile, strMsg
    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    ' Create a File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Check if the specified target file or folder exists,
    ' and build the fully qualified path of the target file
    If objFSO.FolderExists(myPath) Then
        strFile = objFSO.BuildPath(myPath, Mid(myURL, InStrRev(myURL, "/") + 1))
    ElseIf objFSO.FolderExists(Left(myPath, InStrRev(myPath, "\") - 1)) Then
        strFile = myPath
    Else
        WScript.Echo "ERROR: Target folder not found."
        Exit Sub
    End If

    ' Create or open the target file
    Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)

    ' Create an HTTP object
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")

    ' Download the specified URL
    objHTTP.Open "GET", myURL, False
    objHTTP.Send

    ' Write the downloaded byte stream to the target file
    For i = 1 To LenB(objHTTP.ResponseBody)
        objFile.Write Chr(AscB(MidB(objHTTP.ResponseBody, i, 1)))
    Next

    ' Close the target file
    objFile.Close()
End Sub

所有的结果都是一样的。他们不下载文件。有时其中一个会冻结计算机,我必须手动关闭电源。

他们会在一段时间后下载(但不会)并且只将文件显示为 0kb。如果我改变了

objXMLHTTP.Open "GET", strFileURL, False

objXMLHTTP.Open "GET", strFileURL, True

它会立即显示在文件夹中并显示 0kb

无论是对还是错,等待 30 分钟以上对文件大小没有任何影响。实际文件大小为 1,874,886 kB,只需几分钟即可从网站下载。

Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.Navigate2("https://filexxx.exe")
ie.Document.Execwb("saveas", False, "C:\temp\filexxxx.exe")
ie.Quit

给予

第 5 行 char 58 在调用 sub 时不能使用括号

如果我从驱动器\文件夹\文件中删除引号“”,我会得到

第 5 行 char 36 应为“)”

注意 URL 是 https,而不是 http。这几天一直在研究这个。

【问题讨论】:

  • https://file.exe 不是一个有效的 URL,除非有一个 Web 服务器托管一个域 file.exe,您要从中下载索引文档。您的前 2 个代码示例基本相同。 #3 添加了代理处理,只有当你实际通过代理连接时才需要它。 #2 和 #3 不能工作,b/c 他们在保存文件之前不会将响应写入流。 #4 逐字节写入响应。 #5 生成common mistake。除非您的 URL 无效,否则我希望 #1 和 #4 能够正常工作。设置第三... [1/3]
  • [2/3] ... Open 方法的参数为 True 不会有帮助,b/c 将异步运行请求,即您的其余代码不会等待请求返回响应(这就是您最终得到零大小文件的原因)。请原谅我在这里直言不讳,但是在不了解代码实际作用的情况下向问题抛出随机代码 sn-ps 不会让您走得太远。无论如何,就像我说的:#1 和#2 应该适用于 AFAICS。如果他们不需要,您需要... [2/3]
  • [3/3] ... 提供有关您从其中任何一个中得到的任何错误的更多信息,以及 URL 字符串的更好示例。如果必须,请混淆域(最好使用RFC 2602 域名)和文件名,但请保持 URL 的结构不变。
  • 我的意思是“#1 和 #4 应该有效”,当然。
  • 我又打了一次,然后在#4 跑步时上床睡觉。第二天早上醒来,没有wifi。打不开,担心要重新安装windows。今天经过几次尝试,两次完全关闭后,wifi 又可以正常工作了。文件仍未下载。

标签: vbscript


【解决方案1】:

我不知道发布随机代码有什么意义。这段代码的重点是它说明了正在发生的事情。

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Outp = Wscript.Stdout
    Set wshShell = CreateObject("Wscript.Shell")
    Set ShApp = CreateObject("Shell.Application")
    On Error Resume Next
    Set File = WScript.CreateObject("Msxml2.XMLHTTP.6.0")
    File.Open "GET", "https://www.google.com.au/search?q=cat"), False
    File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
    File.Send

        wscript.echo "" 
        wscript.echo "Error getting file" 
        wscript.echo "==================" 
        wscript.echo "" 
        wscript.echo "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description 
        wscript.echo "Source " & err.source 
        wscript.echo "" 
        wscript.echo "HTTP Error " & File.Status & " " & File.StatusText
        wscript.echo    File.getAllResponseHeaders
        wscript.echo    File.ResponseBody

        On Error Goto 0
        wscript.echo "Server Response " & File.Status & " " & File.StatusText
        wscript.echo    File.getAllResponseHeaders
        Set BS = CreateObject("ADODB.Stream")
        BS.type =1
'       BS.Charset = "utf-8"
        BS.open
        BS.Write File.ResponseBody
        BS.Position = 0
        BS.type =2
        BS.Charset = "utf-8"
        wscript.echo    BS.ReadText

        Set BS = CreateObject("ADODB.Stream")
        BS.type = 1
        BS.open
        BS.Write File.ResponseBody
        BS.SaveToFile ShApp.Namespace(&h10).self.path & "\Google.html]", 2
'       wshshell.Run "c:\users\safetyscanner.exe", 1, False

【讨论】:

  • 错误第 7 行预期语句结束。
  • 固定语句结束。结果是我在复制链接时放了 ) 。但不知道它将文件保存到哪里,它像其他尝试发布的 #1 和 #4 一样冻结了我的电脑
  • 是否打印了任何消息。
  • 是的,第 7 行字符 111 预期语句结束。当然,我将链接从 goolge search for cat 更改为我选择的下载链接。
猜你喜欢
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 2021-05-13
  • 2016-07-08
  • 1970-01-01
相关资源
最近更新 更多