VBScript to Save Web File to Computer

This post is an extension from the URL2File script that I posted previously. While the URL2File script allowed you to save web pages to a local file, the following VbScript will allow you to save a binary file on the web to a local file.

This VbScript requires Windows script hosting which you can download from:

http://www.microsoft.com/downloads/details.aspx?FamilyID=47809025-D896-482E-A0D6-524E7E844D81&displaylang=en

Save the following code as save2file.vbs:

'Wscript.Arguments(0) - Website  URL
'Wscript.Arguments(1) - Full File Path

Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2

'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")

'Specify stream type - we want To save binary data.
BinaryStream.Type = adTypeBinary

'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write BinaryGetURL(Wscript.Arguments(0))

'Save binary data To disk
BinaryStream.SaveToFile Wscript.Arguments(1), adSaveCreateOverWrite
Function BinaryGetURL(URL)
  'Create an Http object, use any of the four objects
  Dim Http
'  Set Http = CreateObject("Microsoft.XMLHTTP")
'  Set Http = CreateObject("MSXML2.ServerXMLHTTP")
  Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
'  Set Http = CreateObject("WinHttp.WinHttpRequest")

  'Send request To URL
  Http.Open "GET", URL, False
  Http.Send
  'Get response data As a string
  BinaryGetURL = Http.ResponseBody
End Function

To run the script in a command prompt session:

save2file.vbs [URL] [FULL OUTPUT FILE PATH]
eg. save2file.vbs http://localhost/test.XLS c:\temp\test.XLS

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *