Using Includes in VBScript - Testing the subroutine
(Page 6 of 6 )
We’re going to create two files. The first one, called test.vbs, will be our test script and the second one, called include.vbs, will contain our include code. The first should look like this:
' test.vbs
Import "include.vbs"
Sub Import(ByVal strFile)
Set objFs = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
strFile = WshShell.ExpandEnvironmentStrings(strFile)
file = objFs.GetAbsolutePathName(strFile)
Set objFile = objFs.OpenTextFile(strFile)
strCode = objFile.ReadAll
objFile.Close
ExecuteGlobal(strCode)
End Sub
Our include file is going to be very simple. We’ll just use the Wscript Echo method to create a “Hello World” script.
' include.vbs
Wscript.Echo "Hello World!"
Save both files to the same folder and double click test.vbs. Just like magic, you should get a VBScript message box saying “Hello World!”.

There you have it. A simple subroutine that you can reuse in any of your scripts to add file include capabilities to VBScript. I like to use this to include reusable classes. This way, if I make updates to those classes I can easily update all of my other scripts as well without having to update each one individually.
I hope that you have found this solution as helpful as I did. Find ways to customize it or expand upon it to make it fit your own needs. That’s all for me this time. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |