PaulSadowski.com 

Windows Scripting Host
 VBScript To Save a Binary File from the Internet
This script uses the XMLHTTP object to get a binary file, such as a picture, from a remote host and save it to a file on the local machine. Since WSH does not support binary manipulation of files we use the ADODB.Stream object to handle the saving of the data to a file.

In this example you must specify the filename to save to in the string ImageFile and the directory in which to store that file in the string DestFolder (must exist and must end with a \ character). The remote location of the file is stored in the string URL.

In real world usage you could modify this simple sample to read URLs from a text file, determine the filename from the URL and set the destination path either once or for each URL.

Another usage would be to use the SpecialFolders method to place a file on a user's desktop.
 


'GetRemoteBinaryFile.vbs

ImageFile = "back.gif"
DestFolder = "C:\"
URL = "http://www.paulsadowski.com/images1/back.gif"

Set xml = CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", URL, False
xml.Send

set oStream = createobject("Adodb.Stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Const adSaveCreateNotExist = 1

oStream.type = adTypeBinary
oStream.open
oStream.write xml.responseBody

' Do not overwrite an existing file
oStream.savetofile DestFolder & ImageFile, adSaveCreateNotExist

' Use this form to overwrite a file if it already exists
' oStream.savetofile DestFolder & ImageFile, adSaveCreateOverWrite

oStream.close

set oStream = nothing
Set xml = Nothing

 

 

© 2003 by Paul R. Sadowski   
All Rights Reserved. Used By Permission.  
Comments to: Scripting