VBScript: Download and Save a Binary File

Filed under: Quick Code, VBScript — Written by Chrissy on Wednesday, January 24th, 2007 @ 12:03 pm

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
37 Comments   -
  • Comment by GF | January 24, 2007 @ 2:51 pm

    Why not to use WGET.exe (command line utility), which can be run from VBSCRIPT?

  • Comment by Chrissy | January 28, 2007 @ 8:01 pm

    Hey Gf,
    Powershell works too.. or curl.. ;) But I usually try to use objects that are built-in so that there are no additional downloads required.

  • Comment by bob | February 24, 2007 @ 1:31 am

    WHAT THE HELL IS VBSCRIPT AND HOW DO I DOWNLOAD IT BECAUSE I CAN'T GET ITUNES WITH IT.!!!!!!!!!!!!!!!!

  • Comment by Chrissy | February 24, 2007 @ 5:33 am

    lolz. wtf, bobbles.

    VBScript comes with windows.. you save code to a .txt file, rename it to .vbs and double click. It will launch wscript.exe but srsly, it can damage your system if you run crap code. Your problem will unlikely be solved by figuring out what vbscript is, however. so yeah.

  • Comment by coby | March 25, 2007 @ 10:10 pm

    hi, just like bob here, we cannot get itunes or quick time to install into our computer to use the iPod...can you please help us out because we are very unsure of what to do,

    coby

  • Comment by Bader | April 11, 2007 @ 4:39 am

    Greate litle script. Made the day for me!!

  • Comment by Genn | April 19, 2007 @ 6:02 pm

    I am also having the exact same problems as bob and Coby, but I'm not sure I understand the answer you gave bob. So what do I do to be able to install iTunes? Every time I try, a window pops up saying I have to have VBScript in order to install iTunes. I tried downloading it, but is not working for me. This is VERY frustrating!

  • Comment by Chrissy | April 19, 2007 @ 10:22 pm

    iTunes Users,
    I'm sorry but I cannot help you. I don't use iTunes and I do not have a solution on how vbscript will help you install iTunes. Please go to Apple's forums or keep searching Google for an answers.

  • Comment by Bob Jeffery | April 23, 2007 @ 6:01 pm

    I could not get it to work with the path below, I think it is because I need to pass it a username and pwd. Other than that it is exactly what I am needing.

    Any suggestions on how to pass it user & pwd?

    Thanks!

    http://www.greenecountymo.org/Recorder_FTP/12-26-2006c.zip

  • Comment by Lisa | June 29, 2007 @ 12:55 pm

    can someone please leave detail help on how to get a vb script download or or how to create a script. Like others having problems with itunes

    any help will be greatly appreciated!!!!

  • Comment by David | July 3, 2007 @ 11:28 am

    Nice script! Thanks for sharing.

  • Comment by Sandy | July 12, 2007 @ 6:00 pm

    Cool script !!! I love it !
    Thank you very much

  • Comment by Jason | August 7, 2007 @ 10:22 pm

    Thank you very much. I am using this to automate the installation of our new A/V to VPN users. It is much quicker to have them download the application using an external web server than through the VPN concentrator. Thank you 1000 times over.

  • Comment by Maverick | September 4, 2007 @ 11:40 pm

    Thanks great script

  • Comment by Mihai | October 17, 2007 @ 9:25 pm

    Well, I tried using the script to download an image file from my test web server and save it to my hdd, but it keeps erroring out with "Safety settings on this computer prohibit saving data on another domain", although my test web server is on the local machine. I even turned the whole script into an HTA application, to no avail. I'm using IE7. Any suggestions?

  • Comment by swat | October 25, 2007 @ 5:53 am

    To Mihai:
    It has nothing to do with the location of the installed webserver. The safety settings of IE 7 disallow saving the File with the FileSystemObject (otherwise websites could access your local filesystem >.<). Just click with the right mousebutton on the message and click on allow.

  • Comment by Charles | October 31, 2007 @ 3:23 pm

    WGET - damn linux trolls. Because stringing all the things you might want to do in addition to the download into a command line is just backwards.

  • Comment by Chrissy | October 31, 2007 @ 3:36 pm

    Charles,
    Huh?

    Chrissy

  • Comment by Casey | November 4, 2007 @ 10:34 am

    You are awesome! Thank you, thank you, thank you!

  • Comment by Gabe | November 29, 2007 @ 1:40 pm

    Thanks for the script Works great..

    What if i want to grab an entire folder?

    Thanks

    Gbae

  • Comment by TentHead | December 21, 2007 @ 7:37 pm

    Is there any way to monitor the download progress??

  • Comment by CC | February 5, 2008 @ 11:56 am

    Sweet ass script. Thanks.

  • Comment by MRIS | March 3, 2008 @ 3:02 pm

    Thats a great script. This is a shorter alternative:

    dim ie
    set ie.createobject("internetexplorer.application")
    ie.visible=false
    ie.navigateto("")
    do while ie.busy=true
    wscript.sleep 60
    loop
    ie.document.execwb("saveas",2,)
    ie.quit

  • Comment by Josh | March 18, 2008 @ 7:28 am

    Very useful. Thank you.

  • Comment by mwalstra | April 17, 2008 @ 4:03 am

    Awesome script, this is just what i was needing :)

  • Comment by felipEx | April 19, 2008 @ 11:57 pm

    thanks a lot! was very useful for me ;D

    sorry for ask... i'm really interested to know what plugin do you actually use to dynamically load posts with ajax when you get at page's bottom ('Loading the next set of posts')

    sorry for my crappy english, feel free to mailme =P

  • Comment by Sticks | May 16, 2008 @ 2:53 pm

    Excellent script - just the job !, thanx for sharing.

  • Comment by Skyesblue | June 6, 2008 @ 10:00 pm

    Is it possible to load an http site, locate an href link and click it, acknowledge the dialog save as... box, and indicate the destination directory all through VBScript. It would be so much easier if an FTP site was provided, but it isn't, so I'm trying to take your script and make it do the additional steps, not even sure it's possible, ideas, hints, direction will be appreciated. -rm

  • Comment by Bryan Siew | July 16, 2008 @ 2:54 am

    This is sooooo cool! Thx

  • Comment by James | August 4, 2008 @ 6:49 am

    Very useful,

    thank you

  • Comment by Rui | September 3, 2008 @ 2:06 am

    Works quite well!
    And it´s able to download files from a SP site, for instance, and wget.exe can´t do that...

  • Comment by Amit | September 29, 2008 @ 1:35 pm

    Thanks for providing this script, it really helped me a lot.

  • Comment by Amit | September 29, 2008 @ 1:58 pm

    I have just tested your script and it worked well with small size of file but in case of large file say 500MB it throws following error:
    ADODB.Stream: Out of memory

    Please fix the code and let us know.

    Thank you very much.

  • Comment by Kei | October 28, 2008 @ 6:30 am

    Thats is so POSSIBLE IMPOSSIBLE!
    The text contains VIRUS! Holy moly once i saved as a text the avast pop up and says that it contain virus even that it just a TEXT VBS.

  • Comment by Chrissy | October 28, 2008 @ 8:58 pm

    It's not a virus nor does it contain a virus. A lot of anti virus apps wrongly consider any vbs file a virus. Or maybe it hates FSO. But you can see clearly from the code that it's just downloading a file. If that's a virus, then I download hundreds of viruses a day.

  • Comment by quux | November 7, 2008 @ 4:16 am

    Hi there,

    Nice script - thanks!

    I've refactored it a bit so that it can be called as a simple function which downloads directly to your temp dir. So now you can:

    Call DownloadFile("http://imgs.xkcd.com/comics/faust_20.png")

    And it will download that file to your temp dir. Here is the function:

    Function DownloadFile(DownloadUrl) 'generic file downloader, saves to temp
    'Get name of file from url (whatever follows the final forwardslash "/")
    Dim arURL, FileName, FileSaveLocation
    arURL = Split(DownloadUrl,"/",-1,1)
    If arURL(UBound(arURL)) = "" Then 'if there is a trailing forwardslash
    FileName = arURL(UBound(arURL) -1)
    Else
    filename = arURL(UBound(arURL))
    End If
    'Get temp folder location
    Dim oFS, TempDir
    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set TempDir = oFS.getSpecialFolder(2)
    Wscript.Echo TempDir & "\" & FileName
    FileSaveLocation = TempDir & "\" & FileName

    ' Fetch the file
    Dim oXMLHTTP, oADOStream
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    oXMLHTTP.open "GET", DownloadUrl, false
    oXMLHTTP.send()
    If oXMLHTTP.Status = 200 Then
    Set oADOStream = CreateObject("ADODB.Stream")
    oADOStream.Open
    oADOStream.Type = 1 'adTypeBinary
    oADOStream.Write oXMLHTTP.ResponseBody
    oADOStream.Position = 0 'Set the stream position to the start
    If oFS.Fileexists(FileSaveLocation) Then oFS.DeleteFile FileSaveLocation
    Set oFS = Nothing
    oADOStream.SaveToFile FileSaveLocation
    oADOStream.Close
    Set oADOStream = Nothing
    End if
    Set oXMLHTTP = Nothing
    End Function

  • Comment by quux | November 7, 2008 @ 4:38 am

    Something's up with WP. When I try to post a link to a better-formatted version of the script above, I get redirected to a blank page. Sorry if I just dumped like 20 bogus posts into your approval queue (if you have one).

Leave your comment