Tuesday, April 5, 2005

Copying files from internet using VBS

I got a question on how to do this from my fellow worker Robert and thought I should put the answer here.
One way is using the XMLHTTP object combined with adodb.stream like this using VBScript:

' Requirements: ADO 2.5+ and IE 5+
sSourceUrl = "http://www.jtbworld.com/images/"
sDestFolder = "c:\temp\"
sImageFile = "jtbworld.gif"
Set oXMLHTTP = WScript.CreateObject("Microsoft.XMLHTTP")
oXMLHTTP.open "GET", sSourceUrl & sImageFile, False
oXMLHTTP.send
Set oStream = CreateObject("adodb.stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
oStream.Type = adTypeBinary
oStream.open
oStream.write oXMLHTTP.responseBody
oStream.SaveToFile sDestFolder & sImageFile, adSaveCreateOverWrite
Set oStream = Nothing
Set oXMLHTTP = Nothingx

1 comment:

  1. Restored comments

    Robert Velén said...

    "Fellow worker Robert" tested this. Unfortunatly the anti virus software deleted the vbs file as soon as I saved it. After talking to Jimmy, it appears the same technique is used by viruses.

    /Robert
    April 05, 2005

    ReplyDelete