Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBScript FAQ

WSH Issues - General

How Can I Do a Script "Include" in WSH? by dilettante
Posted: 8 Apr 03 (Edited 13 May 03)

There are (at least) two ways to do this.  One requires that you write your "main" script as a Windows Script File (.WSF) instead of a naked VBScript file (.VBS), while the other does not.

You should look into WSH's .WSF file format, it offers many advantages, this one among them.

The examples here presume you want to make a logon script.  Though they aren't practical logon scripts because they use MsgBox and do silly things, they illustrate the point.

File to be included:

Option Explicit
Const cstrHead = "Veeblefester Logon Script 1.0"

Sub Announcement()
  MsgBox _
      "Welcome to Veeblefester Widgets, Inc. logon script." & vbNewline _
    & vbNewLine _
    & "We hope you'll have a pleasant logon experience." & vbNewLine _
    & "Be sure and stop by again soon for the finest logon" & vbNewLine _
    & "you'll encounter anywhere!", _
    vbOKOnly, _
End Sub

Function Magic(lngX)
  Magic = (lngX + 34) / 12
End Function

The modern way:

<job id="Includer">
  <script language="VBScript" src="SnazzyInclude.vbs"/>
  <script language="VBScript">
    Option Explicit
    Dim lngStory

    lngStory = 13245768
    MsgBox CStr(Magic(lngStory)), vbOKOnly, cstrHead

The old, alternative method:

Option Explicit
Const ForReading = 1
Dim objFSO, tsInc, strInc, lngStory

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set tsInc = objFSO.OpenTextFile("SnazzyInclude.vbs", ForReading)
strInc = tsInc.ReadAll
Set tsInc = Nothing
Set objFSO = Nothing
Execute strInc

lngStory = 13245768
MsgBox CStr(Magic(lngStory)), vbOKOnly, cstrHead

As I said, you probably won't want to use MsgBox calls in a logon script, but you get the idea.

The first approach uses a .WSF script.  These run with CScript.exe as well as WScript.exe just like a .VBS, .VBE, .JS, etc.  It uses the src attribute of the <script> element to perform the include.

The second approach relies on reading the include file via the FSO and using the VBScript Execute statement.  You might want to look into the limitations and possible side-effects associated with Execute before using it.

But choice is good, right?

Back to VBScript FAQ Index
Back to VBScript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close