Tap this automated method for using a script to drop “unsubscribe” customers from your email lists.

Yanking Data from an Active Server Page

Tap this automated method for using a script to drop “unsubscribe” customers from your email lists.

Hmm… something’s different. There’s a new column title, a new picture (but the same goofy mug). Perhaps you’re wondering where this month’s featured Windows utility is. “What’s this ‘Scripting for MCSEs’ stuff?” Well… as Kevin Costner’s manager in Bull Durham said, “The league’s decided to make a change.” Besides, when the editor-in-chief herself calls with an idea for a new direction for your column, you either get on board or you get traded to Visalia as the new bat boy. Fortunately for me (and Visalia), I’m very excited about this new column. Yep! We’re going to the Show. The Major Leagues. The World Series of Scripting! The… but I digress.

The good news is we’ll still be showing you ways to simplify your collective administrative lives. The better news is that we’ll be focusing on more powerful solutions made possible via Windows NT’s (and Windows 2000’s) enhanced scripting abilities. We’re going to look at scripting from a challenge/solution perspective. Solutions will run the gamut from simple scripts that leverage third-party utilities and components, to complex “code-o-ramas” guaranteed to make even the most GUI-dependent server jockey feel like a born-again code monkey!

We’ll start by looking at some of the tools we’re going to use on this adventure, then writing a script or two. Nothing like jumping in with both feet!

WScript.exe / CScript.exe

These are the scripting hosts. By default they have engines for VBScript and JScript, but you can add more if necessary. The examples you’ll see in these pages will be VBScript (though I may occasionally throw in some JScript—just to keep you on your toes).

Notepad.exe

The quintessence of script editors! Notepad is where we create our magic. Feel free to use another environment if you want, but you can’t beat Notepad. It has no syntax checking, no spell checking, and it doesn’t even have Replace functionality. Not to mention the fact that it tries to save everything with the .TXT extension. So why do I use it? It’s a bad habit I can’t seem to break.

VBS file extension

If we manually start each script using either WScript or CScript, we can give the script files any extension, so long as we have that extension associated with a script engine. However, the WSH registers the .VBS and .JS extensions as VBScript and JScript, respectively. If we use these extensions, we can execute the scripts simply by double-clicking on the file. Remember not to do this if your script is expecting command-line arguments, or you’ll get an error.

Active Server Pages

No, this isn’t going to be a column on Web site development. Nevertheless, there are times when our administrative tasks may require the services of an Internet/intranet server. Indeed, this month’s script is a perfect example. When we find our administrative tasks crossing paths with our Web servers, the scripts will include the relevant ASP code.

In this column (stay with me), we’re going to write a script to create and add to a text file. While this seems simple enough on the surface, there are a few hurdles to leap when attempting to automate it inside a script. Let’s look at a common scenario…

The Situation

Your company sends regular email updates to your customer base, informing them of the latest product/service/doohickey that you offer. Even though these are registered customers, you must provide a way for them to be removed from your mailing list lest ye be accused of spamming. Short of purchasing an expensive electronic mail system that does this automatically, you need to find a way to keep track of these “removes” so that you no longer mail to them.

Your emailing program uses a text file containing email addresses to handle delivery. It also uses tags that can reference any data you want in the message itself (usually this is used for names, but you’ve decided to embed the Contact ID instead). This file has already been properly formatted.

You write your email and add the link at the end for the customer to click if he or she wishes to be removed. The link looks something like this:

http://www.mycompany.com/email/remove.asp?

The email software replaces the tag with the text you specified when it delivers the message. In this case, the text is:

CID=12345&Email=me@you.com

When the customer receives the email, the link is complete:

http://www.mycompany.com/email/remove.asp?CID=12345&Email=me@you.com

Our challenge lies in telling the Active Server Page to add the Contact ID and email address for each “remove” into a comma-delimited file. (We already have software to parse that file and remove each person from the email database.) In order to make this happen, we need to use the Windows component “Scripting.FileSystemObject” and the ASP component “Request.QueryString.”

Request.QueryString returns the data associated with a label that is passed to the Active Server Page—in other words, everything that comes after the “?” on the URL. The labels in this case are “CID” and “Email”. To accomplish this, we add the code in Listing 1 anywhere inside the file “remove.asp”.

Listing 1. This is the code that you insert into the file "remove.asp".
  < ' this="" tells="" iis="" that="" the="" script="" starts="">
Const ForAppending=8
' The VBScript code to append to the open file
Set objFSO=Server.CreateObject
(“Scripting.FileSystemObject”

' The "True" flag in the next line tells IIS to
' create the file if it doesn't already exist
Set objFile=objFSO.OpenTextFile(“c:\removes\remove.txt”, ForAppending, True)

' Get the data from the ASP query string and format it,
' separating the fields with a comma
strLine=Request.QueryString(“CID”) & "," & Request.QueryString(“Email”)
ObjFile.WriteLine strLine  ' Write the line
ObjFile.Close              ' Close the file

' Exit the script
%>

Every time a user clicks the “remove” link, it adds the user name and contact ID to the file “remove.txt,” which is used to handle that actual removal from the database. (There are more complex ways of doing this directly, but for now let’s keep it simple.)

As we go along each month, I’ll explain the rules and conventions I use when writing scripts. Some are required and some are simply the “standard.”

Variable Names

I always precede each variable with an indication of its type:

  • obj—Object
  • str—String
  • b—Boolean (yes/no or True/False)
  • i—Integer

I’ll cover other variable types as we encounter them.

Objects

All objects (objObjectName) are created with “Set” and “CreateObject” (or Server.CreateObject with ASP). The exception to this is when a method of one object returns another object, as in line 5 of our script. The OpenTextFile method of the FileSystemObject returns a TextStream object. Dealing with objects is one of the more difficult concepts when scripting. I’ll do my best to explain the rules as we go and thus avoid any unnecessary headaches.

Scripting is powerful. It’s also a lot of fun. Batter up!

About the Author

Chris Brooke, MCSE, is a contributing editor for Redmond magazine and director of enterprise technology for ComponentSource. He specializes in development, integration services and network/Internet administration. Send questions or your favorite scripts to chrisb@componentsource.com.

comments powered by Disqus
Upcoming Events

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.