|
|
|
How to Use VBScript to Download a Web PageAutomatically Downloading a File from the Internet
A web page can be downloaded very easily to a computer by using VBScript and some of the objects built into Microsoft Windows
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:
And the programmer can do all of that surprisingly easily. Using VBScript to Request a Web Page from a Web ServerVBScript, 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 PageThe 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. SummaryA 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:
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.
|
|
|
|
|
|
|
|