Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I Do a Script "Include" in WSH?

WSH Issues - General

How Can I Do a Script "Include" in WSH?

by  dilettante  Posted    (Edited  )
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:

[color blue]SnazzyInclude.vbs[/color]
Code:
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, _
    cstrHead
End Sub

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

The modern way:

[color blue]Includer.wsf[/color]
Code:
<job id="Includer">
  <script language="VBScript" src="SnazzyInclude.vbs"/>
  <script language="VBScript">
    Option Explicit
    Dim lngStory

    Announcement
    lngStory = 13245768
    MsgBox CStr(Magic(lngStory)), vbOKOnly, cstrHead
  </script>
</job>

The old, alternative method:

[color blue]OtherWay.vbs[/color]
Code:
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
tsInc.Close
Set tsInc = Nothing
Set objFSO = Nothing
Execute strInc

Announcement
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?
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top