通过VBS下载文件

时间:2018-07-17 01:17:53

标签: vbscript

我知道已经有人问过并回答了,这是我找到启动项目代码的地方。但这不起作用。

我被困住了。我已经尝试了很多,但代码却无法正常工作。

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调用子程序时不能使用括号

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

  

第5行char 36预期为“)”

请注意,URL是https,而不是http。现在已经为此工作了几天。

1 个答案:

答案 0 :(得分:0)

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

    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