Using "include files" in VBScript Programs
FOREFRONT TECHNOLOGIES

Using "include files" in VBScript Programs

By: Forefront Technologies - 10/9/2015

“Include files” are a useful part of programming.  They allow for common code, typically functions and subroutines, to be packaged into libraries (i.e., files in this case).  The libraries can then be shared by multiple programs without having the actual library embedded in the main program file.  Another advantage is that you will have one version of the code in the libraries.  When you make a correction or enhancement to a routine in the library, all programs that include the library can take advantage of it.  Unfortunately, VBScript does not directly support “include files”.

You’ve probably seen other articles on implementing “include” files in VBScript, however they usually involve a subroutine for implementing the include files which means you need to include the “include file subroutine” into any program that needs to use “include files”.  While this method works, it partially defeats the purpose.

Here are some examples of using include files in other languages:

HTML: <!--#include file="path-spec"-->

C/C++: #include "path-spec" or #include <path-spec>

JAVA: #include <path-spec>

PHP: include 'path-spec';

As stated above, this type of “include file” is not supported by VBScript.  Of course if your using VBScript in an ASP program you can use the HTML format outside of the ASP code to include HTML files and ASP files, but in plain vanilla VBScript, you can’t.

Take the following example into consideration.  Let’s say you’d like to write a routine to print the date, time, and a message to the log device, and you’d like to use this routine in all your VBScript programs.

Without using an “include file” you’d have to do something like the following:

'
' Main Program
'
writeLog "Hello world! "

sub writeLog(msg)
    wscript.echo now & ": " & msg
end sub

If you wanted another program to use “writeLog” you’d have to include the code for that subroutine in that program too.  This introduces a maintenance nightmare if you want to make a change to “writeLog”.  Instead, create a file named “writeLog.vbs” or “myLibrary.vbs” (or any name you’d like) and in this file place the code for the “writeLog” subroutine.

Next, you can add the following to any VBScript program that you’d like to use the “writeLog” subroutine in:

execute "incFile = ""path-spec"":Set objFS = CreateObject(""Scripting.FileSystemObject""):set objIFile = objFS.GetFile(incFile):set objITS = objIFile.OpenAsTextStream(1, 0):Execute objITS.Read(objIFile.Size):objITS.Close:set objITS = nothing:set objIFile = nothing:set objFS = nothing"

Change path-spec to the file name you saved your subroutine in (e.g, ""myLibrary.vbs"").   Also note that the line above should not wrap when you put it into your VBScript program, it should all be on one line.

Example of a main program “mainProgram.vbs”:

'
' Main Program
'

execute "incFile = ""myLibrary.vbs"":Set objFS = CreateObject(""Scripting.FileSystemObject""):set objIFile = objFS.GetFile(incFile):set objITS = objIFile.OpenAsTextStream(1, 0):Execute objITS.Read(objIFile.Size):objITS.Close:set objITS = nothing:set objIFile = nothing:set objFS = nothing"

writeLog "Hello world!"

Example of the “include file” “myLibrary.vbs”:

sub writeLog(msg)

    wscript.echo now & ": " & msg

end sub

That’s all it takes.

Some additional things to note are:

  1. You can add additional subroutines and functions to your library.
  2. You can create a separate folder to store your include files.  Just make sure to specify the path in ""path-spec"".  Example: ""c:\vbscript\libraries\myLibrary.vbs""
  3. You can use multiple include files in your VBScript program.

Here are some links to the code as ".txt" files.  To use them, save them as ".vbs" files.

mainProgram.vbs.txt
myLibrary.vbs.txt