Download file from vbscript?

I need a script that can be run on freshly installed windows xp+ and download specified files from internet, like http://www.python.org/ftp/python/2.6.2/python-2.6.2.msi Is it any easy way to do it without hand-crafting HTTP/FTP requests or using third-party programs like wget? I can suggest that WScript.CreateObject("internetexplorer.application") will do the magic, but documentation on it is extremely huge and Google is silent, as always :).


Just found this one pasted below. You can run it with cscript and have it scheduled.

   'Set your settings
strFileURL = "http://www.domain.com/file.zip" strHDLocation = "D:\file.zip" ' Fetch the file Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") objXMLHTTP.open "GET", strFileURL, false 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 'Set the stream position to the start Set objFSO = Createobject("Scripting.FileSystemObject") If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation Set objFSO = Nothing objADOStream.SaveToFile strHDLocation objADOStream.Close Set objADOStream = Nothing End if Set objXMLHTTP = Nothing

VBScript is annoying unco-operative when it comes to doing things like this. This sort of thing always requires COM servers that aren't formally part of the scripting engine.

Maxwell's suggestion of using MSXML2.XMLHTTP seems a cool way round this. I must admit I hadn't come across that trick before. There are also various commercial COM servers that will do file downoads.

This type of problem is one of the reasons I'm looking at shifting much of my VBScript to Powershell. Because Powershell can seamless use .Net objects, doing such things is a lot easier.

JR


The method using

Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

is more elegant, unfortunately is not very portable since the version of the object changes a lot throughout the versions and updates of Windows.

Using

WScript.CreateObject("internetexplorer.application")

is probably better but less elegant.


MSXML2.XMLHTTP is built around the old WinInet. There's a newer interface called Msxml2.ServerXMLHTTP, which is more reliable. It is built on a newer MS WinHTTP stack. I suggest calling WinHTTP directly.

Just use CreateObject("WinHttp.WinHttpRequest.5.1") instead of creating an MSXML2.XMLHTTP object. The rest of the code stays the same.