VBScript: Download and Save a Binary File

Update: If you are an iTunes user that needs VBScript, this page won’t help you. Please visit the following two sites for two different solutions: Apple Docs and KeathMilligan.net.

This is an old script I dug up which I call “fileFetch”. It’s a script that downloads a binary file using XMLHTTP and saves it using an ADO stream. This is handy for nightly downloads and will work within SQL Server DTS packages.


‘ 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

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in VBScript