title: Download file via vbs
body: <p>I know this has been asked and answered, it is where I found the code to start my project.  But it doesn't work.</p>
  
  <p>I'm stuck.  I have tried and tried and the code(s) don't work.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And this one.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And this one.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And this one</p>
  
  <pre><code>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
  </code></pre>
  
  <p>All have same result.  They don't download the file.  Sometimes one of them freezes the computer and I have to manually power down.</p>
  
  <p>They do, after time, download (but don't) and only shows file as 0kb. If I change</p>
  
  <pre><code>objXMLHTTP.Open "GET", strFileURL, False
  </code></pre>
  
  <p>to</p>
  
  <pre><code>objXMLHTTP.Open "GET", strFileURL, True
  </code></pre>
  
  <p>It instantly shows up in folder and shows 0kb</p>
  
  <p>Either True or False, waiting 30+ minutes does nothing to size of file.  The actual file size is 1,874,886&nbsp;kB and only takes a couple minutes to download from website.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Gives</p>
  
  <blockquote>
    <p>line 5 char 58 cannot use parentheses when calling a sub</p>
  </blockquote>
  
  <p>And if I remove the quotation marks "" from drive\folder\file I get</p>
  
  <blockquote>
    <p>line 5 char 36 expected ")"</p>
  </blockquote>
  
  <p>Note the URL is https, not http.  Been working on this for couple days now.</p>