Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 :).

share|improve this question
add comment

4 Answers

up vote 12 down vote accepted

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
share|improve this answer
    
+1 for streamline code. This looks much more efficient than using WScript.CreateObject("internetexplorer.application"). –  KevinH Jun 22 '09 at 13:30
add comment

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

share|improve this answer
    
I second migrating to PowerShell if at all possible. While it isn't a direct answer to the original question - in most cases it would be the right path forward. –  Goyuix Sep 13 '12 at 14:44
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.