RSS

VBScript: Faking the include directive

31 Mar

VBScript does not provide any include directive that is common in many other languages. Luckily, the language provides a pseudo-include statement, EXECUTEGLOBAL and EXECUTE, which we can use to fake the include directive.

Why do we need to do that in our scripts? Well, very often, we want to distribute a standard set of codes, for example, templated functions and procedures. You want to ensure that all your scripts are running from the same versions. If those functions are included inside the vbscript, then it will be impossible to control your standard functions’ version.

A real life example: I have a report class which is used by various Start of Day (SOD) scripts to provide a summary text file about their status. I don’t want to copy the codes into each script and have to update every script everytime I add or update the function of the class. Includes come in handy in this case.

I created a function that would read the file to be included and use EXECUTEGLOBAL to fake the include. You only need to copy this function into all the scripts that you intend to use include statements once. In all the headers of your script, you can add the following line:

‘pseudo-include directive
EXECUTEGLOBAL GetIncludeFile(“c:\scripts\myclass.vbs”)

In your script you should have this function:

Function GetIncludeFile(FileToInclude)
dim fso, txt

Set fso = CreateObject(“Scripting.FileSystemObject”)
set txt = fso.OpenTextFile(FileToInclude, 1)
GetIncludeFile = txt.ReadAll
txt.close

set txt = Nothing
set fso = Nothing

End Function

Advertisements
 
Leave a comment

Posted by on March 31, 2008 in Scripts, VBScripts

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: