Suite101

How to Use VBScript to Download a Web Page

Automatically Downloading a File from the Internet

© Mark Alexander Bain

Jan 12, 2009
Use VBScript to Automatically Download a Web Page, Mark Alexander Bain
A web page can be downloaded very easily to a computer by using VBScript and some of the objects built into Microsoft Windows

Editors' Choice

One big advantage to using the Linux operating system is the easy with which programmers can produce scripts that are able to download and process web pages. However, the Windows programmer can create such scripts just as easily. That is, of course, if they are using VBScript. With VBScript a Windows programmer is able to:

  • request the contents of a web page from a server
  • save the results of the request as a web page on the local computer

And the programmer can do all of that surprisingly easily.

Using VBScript to Request a Web Page from a Web Server

VBScript, as always, make use of the objects built into Microsoft Windows, and in this case the object to be used is the XMLHTTP object:

sub get_html (up_http, down_http)
dim xmlhttp : set xmlhttp = createobject("msxml2.xmlhttp.3.0")
xmlhttp.open "get", up_http, false
xmlhttp.send

Here the web page to be downloaded is fed as a variable to a subroutine. The subroutine creates an XMLHTTP object and then uses this to request the web page from the server. The results of the request can then be used to create the web page on the server.

Using VBScript to Save a Web Page

The response from the server is loaded as text and this text can be saved to a file by making use of the File System object:

dim fso : set fso = createobject ("scripting.filesystemobject")

The file system object is uses to create the new file:

dim newfile : set newfile = fso.createtextfile(down_http, true)
and the text from the XMLHTTP response can then be written to the file:
newfile.write (xmlhttp.responseText)

the file must then be closed:

newfile.close

Finally any memory used by the subroutine can be released:

set newfile = nothing
set xmlhttp = nothing
end sub

The programmer can then run the subroutine by calling it with the URL for the web page to be downloaded along with the file name that it is to be saved to:

get_html _
"http://www.suite101.com/writer_articles.cfm/linuxtalk/index.html", _
"c:\downloads\articles.html"

If this code is saved to a .vbs file then it can be run by double clicking on the file via Windows Explorer, and the web page will be copied to the user's computer.

Summary

A programmer can easily use VBScript to download a web page from a web server to a Windows computer. In order to do this they will need to create a .vbs file that:

  • uses the XMLHTTP object to request the contents of web page from a server
  • uses the file system object to create a new file to which the results of the request can be written

Just by using these objects with VBScript the programmer can copy a web page to their own computer ready for further processing or just as a backup.


The copyright of the article How to Use VBScript to Download a Web Page in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Use VBScript to Download a Web Page in print or online must be granted by the author in writing.


Use VBScript to Automatically Download a Web Page, Mark Alexander Bain
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo