nerds:~ #

24Jan/0768

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
Posted by: Chrissy   Filed under: VBScript Leave a comment
Comments (68) Trackbacks (0)
  1. Malo, how can you tweak this code to download a file from a SharePoint Document Library? Do you have an example you can post here? Thanks!

  2. Never mind, Malo… I just discovered that all of my frustration has been due to the SharePoint server blocking MDB files for security reasons. GRRRR!!!! I renamed the file to a TXT file instead of an MDB file and uploaded the TXT version to sharepoint. The script worked just fine as is. Thanks for the script!

  3. is it feasible to add an authentication part into the script if a username & pwd is required?!?
    thanks!

  4. I am developing a website and need to allow some files to be download by viewers of the website. These files are .XLS (Excel), PDF and ZIP (combinations of XLS and PDF). Iwould like to be able to download them without the usual “Open”, “Save” or “Cancel” dialog.
    I also need to work arount the Server 2003 4mB limit problem.
    Do you have something or would you be willing to develope something for a fee? If so what would you charge?

  5. Thanx Chrissy!! Saved me som troublish time… You’re the best!!

  6. I will dedicate this function to you!

    Public Function ChrissyFileFetch(strFileURL, strHDLocation)
    [...your code...]
    End Function

    :)

    See U!

  7. haha thanks, Fredrik!

  8. Hey Earl,
    Sorry, my plate is full currently. :\

    Chrissy

  9. Great Script. My issue is that I do not have just one file such as file.zip to down load, I need to download several thousand image files on the fly. I have the URL paths and photo names to each in my database already so I need to dynamically populate the script usign T-Sql. I figure I need to use the FOR EACH…NEXT but I am not getting this done correclty

  10. actaully I figured it out.

  11. Hello,

    I am trying to download a word document from sharepoint using the URL, but a corrupted file is being downloaded. I have used the same code as given above. Please anyone help me how to download file from sharepoint.

  12. Nice script, Chrissy. Helped solve some problems. Thanks!

    Seb, I added/changed Chrissy’s script to do http Basic Authentication like this.

    key = base64_encode( username & “:” & password )
    xmlhttp.open “GET”, url, False, username, password
    xmlhttp.setRequestHeader “Authorization”, “Basic ” & key
    xmlhttp.send

    where base64_encode is a sub to do base64 encoding.

  13. Hi All,

    Realy great script, but my question is “How to download files (xls) from web link that require SSO authentication”. Please help me out.

    I tried using the code above, and passed sso id and password, but its downloaded the Login page in excel.

    Please provide some hint, thanks in advance.

    Thanks & Regards
    Amit

  14. How to rewrite a file in the destination?

  15. Excellent Script……I am able use it to automate downloading list of PDF files…..from webpages…

  16. THANKS A BUNCH CHRISSY.

    THIS IS VERY VERY SIMPLE SCRIPT AND WORKS GREAT.

    YOU ROCK!!!

  17. Is there a way to download a file in bits and pieces ? I have a file 0.5GB and it continue getting out of memory errors. Is there a way to page files or so ?


Leave a comment


No trackbacks yet.