Scripted HTTP Downloads

Automate retrievals of new utility versions

Dick Lewis

April 11, 2004

9 Min Read
ITPro Today logo in a gray background | ITPro Today


Every once in a while, I discover a useful utility on the Internet. After I've used the utility for a while, I like to make sure that I'm still using the most current version. However, I don't want to have to constantly monitor the originating Web site for newer versions. One popular utility that I use extensively is McAfee's Stinger, a standalone virus-scanning tool. This effective tool's only drawback is that McAfee updates Stinger infrequently, and determining when a new version will be available is impossible.

Obviously, Stinger's release frequency depends on the number of emerging virus-attack threats. I had used the WatchThatPage site (http://www.watchthatpage.com) to monitor the Web page for changes, but I still needed a better way to learn about new versions of the file and, further, I wanted the latest version delivered to my PC mailbox automatically. Out of this desire was born my HTTPDownLoader.bat script, which you can download from the Windows Scripting Solutions Web site. (Go to http://www.winnetmag.com/windowsscripting, enter InstantDoc ID 42106 in the InstantDoc ID box, then click the 42106.zip hotlink.) Although I designed this script primarily for the Stinger utility, I've made it generic so that you can easily configure it for downloading any HTTP-available file that you need to regularly check for updates.

To download and compare Stinger versions and sizes, the script uses two new utilities that I haven't covered before. It also uses the old reliable Blat utility, which I use for sending out the email message with the file attachment.

HTTP Downloads
Although FTP file downloads are possible from the command shell, internal commands and resource kit tools don't support HTTP file downloads. Therefore, I turned to the ever-increasing number of third-party tools on the Internet and located one that you'll want to add to your list of resources. (For a listing of third-party command-shell tool resources, see the sidebar "Command-Shell Tool Resources," page 15.) The iOpus File Downloader is what I use to download the Stinger file. The iOpus File Downloader might appear to be a GUI tool when you first launch it, but it does feature command-line capability and can run without user interaction. The tool uses the following syntax for downloads:

downloader.exe -download    source destination

where source specifies the pathname of the file you want to download and destination specifies the directory in which you want to copy that file. For example, the command for Stinger might look like

downloader.exe -download    http://download.nai.com/    products/    mcafee-avert/stinger.exe    D:stinger.exe

Note that the current version of iOpus File Downloader (which I tested) supports only a local destination directory. If you use a Universal Naming Convention (UNC) path, you'll get some unusual copy results. I pointed out this flaw in a bug report, and iOpus plans to fix the problem in a future version. For now, stick with a local path (e.g., D:stinger.exe), then copy the file to a remote location if necessary. With HTTPDownLoader.bat file, this limitation isn't a concern.

Version Comparison
Now that you have a means for downloading the file from the Web, you need to be able to determine whether the new downloaded version differs from a previous version you might already have. For comparing file versions, I use the Windows 2000 Support Tools utility Filever, which permits comparisons of folder structures and individual files. The HTTPDownLoader.bat script downloads the current version from the Web site, then uses Filever to determine whether it matches any previous copies you already have. If the version or byte count has changed, the script sends an email message stating that the file has changed and—if the recipient has requested it—sends the new version as an attachment.

Here's the basic Filever command that the script uses:

filever.exe /A D:stinger.exe

In this command, the /A switch tells the utility to not display file attributes. As the excerpt of code in Listing 1 shows, the script uses the For command to extract the version and size information. Note that I've simply combined the version (token 4) and size (token 6) information. You might ask why you need to check both version and size. First, the developer might have changed the file's content but not the file's version number. Second, you might find that the file has no version number at all or that a file's content has changed but not its size. Callout A in Listing 1 shows the comparison code. The assumption is that if the file has changed on the source Web site, it's a newer version.

Here's an important tip for using utilities inside For commands: Filever and certain other utilities might work inconsistently or incorrectly if you enclose their paths inside double quotation marks in a For command. Instead of surrounding the path in double quotes—the typical practice for handling spaces—you need to find a path for the utility that has no spaces. Unfortunately, the default locations for many tools, including most Microsoft tools, contain spaces. You might even have a utility that once worked fine inside a For command until you used double quotes to enclose its path, at which point you began receiving error messages such as The filename, directory name, or volume label syntax is incorrect. I always recommend that you install resource kit tools, Support Tools, and any other utilities into a folder that contains no spaces in its path (unless you want to constantly convert all your paths into 8.1 format).

You've Got Mail
The tool I use to send mail attachments is the simple and flexible Blat utility. The version of Blat that's current as of this writing is 1.9.4, but this code should also work fine with earlier versions. However, you need to be aware of a couple of Blat idiosyncrasies. First, if you try to access the online Help file by typing

blat /?

or

blat /help

in the command-shell window, you'll get an error message. So, to see the verbose option list, just type

blat.exe

in the command-shell window. Blat has an intimidating number of options. However, HTTPDownLoader.bat uses only some of them. If you were to type the Blat command that the script uses in the command-shell window, it would look like

blat D:Fblat.txt    -attach D:Stinger.exe    -s "New Stinger Version    Attached"    -server mail.yourcompany.com    -f [email protected]    -t [email protected]

This command's argument specifies the path to the text file that contains the body of the email message. The ­attach switch specifies the file you want to attach, the ­s switch specifies the subject line, the ­server switch specifies the SMTP mail server's address, the ­f switch specifies the email address of the person sending the email message, and the ­t switch specifies the email address of the intended recipient. Callout A in Listing 2 shows how this Blat command looks when you use variables rather than hard-coding the argument and switch values. The code at callout A sends an email notification about the utility's new version and attaches that new version. The Blat command above callout A sends only the email notification.

The second idiosyncrasy, which is obvious in the preceding command, is the necessity for a text file to contain the text of the message. Even if your message is extremely short, you can't enter it as an argument; you must place it in a text file that Blat uses as the message source.

Prelaunch Considerations
To benefit from HTTPDownLoader.bat, you need to heed a few prerequisites and considerations:

  • The target file (i.e., the online file for which you're checking the version and size) must have a name and URL location that doesn't change with version changes.

  • The target file should be moderately sized. You wouldn't want to waste bandwidth downloading a 100MB file every day to compare it with a version you've previously downloaded. For example, the Stinger utility is about 700KB, so its size is ideal.

  • The script compares files based on file version and size in bytes. An alternative would be to perform the comparison based on file version alone, but I opted to use both characteristics.

  • Understand that some users in your environment might not want to receive the new file as an attachment. They might simply want to know when the file has changed so that they can download it from the source site or from a shared folder location on your internal network.

  • If you block .exe file attachments in your internal network, you might want to just send out an email notification and point users to a shared folder that holds the new file.

Getting the Script to Work
I've tested the HTTPDownLoader.bat script on Windows XP Professional Edition Service Pack 1 (SP1) and Win2K SP4 systems. To use HTTPDownLoader.bat in your environment, you first need to create FBlat.txt, which contains the text for the email notification. You then need to perform the following steps to configure the code that Listing 3 shows:

  1. Configure the Tools variable with the pathname of the folder in which the filever.exe, blat.exe, and downloader.exe tools reside. Don't use double quotes or spaces in this path.

  2. Set the HTTPloc variable to the Web location of the file you want to retrieve.

  3. Configure the Notes variable with the Web location of the release notes for the file.

  4. Configure the Recipients variable with the recipients who require only email notification (i.e., no file attached). Separate recipients by using commas with no spaces.

  5. Set the FRecipients variable to the recipients who require an email notification with the file attached. Again, use commas with no spaces to separate recipients.

  6. Configure the PRecipients variable with the recipients who should receive an email notification if the file download fails. An example of such a recipient would be the script administrator or another point of contact who needs to know that the URL might have changed. Again, use a comma-separated list.

  7. Set the From variable to the email address of the person sending the email notifications.

  8. Configure the SMTPsrvr variable with the address of the SMTP mail server.

No further configuration to HTTPDownLoader.bat is necessary unless you plan to copy the new file to a share location or you need to specify individual locations for the utilities.

HTTPDownLoader.bat automatically determines the folder in which it resides, then assumes that this folder is where you want to store downloaded file versions and temporary Blat files. Unless you have different storage plans, you won't need to configure a location for these items. If you want to transfer a copy of the new file from this directory to a shared folder location, you'll need to add a line of code to the script's Mailit section, following the instructions in the commented code in Listing 2.

Final Tasks
After you get everything working and send some test messages to yourself, you can add in all your recipients and add this script as a job in Task Scheduler. Contact any support people that might need the file you're downloading—in this case, the stinger.exe file—and add them to the distribution list (DL). Keep your eyes open for any other files that might need similar periodic version checks, and let your automated HTTP download scripts do the work for you.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like