How do I include a common file in VBScript (similar to C #include)?
Asked Answered
E

8

49

VBScript doesn't appear to have a way to include a common file of functions.

Is there a way to achieve this?

Excruciation answered 25/11, 2008 at 2:23 Comment(1)
Here's a similar question: #928737Steamheated
G
52

The "Windows Script Host" framework (if ya want to call it that), offers an XML wrapper document that adds functionality over regular vbs files. One of which is the ability to include external script files of both the VBscript and Jscript flavors. I never got very deep into it, but I think it would do what you're wanting to do. http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx

You can include JavaScript, VBScript, or modules of other WScript script languages.

Example WSF file:

<job id="IncludeExample">
   <script language="JavaScript" src="sprintf.js"/>
   <script language="VBScript" src="logging.vbs"/>
   <script language="VBScript" src="iis-queryScriptMaps.vbs"/>
</job>

If the above file is called "iis-scriptmaps.wsf", run it this way with cscript.exe:

cscript.exe  iis-scriptmaps.wsf
Gillum answered 25/11, 2008 at 2:23 Comment(4)
Yeah, this is definitely the way to go. I used wsf files all the time in the past to include script libraries.Dionne
So can you run the wsf programs the same as the vbs ones (i.e., is there an association based on the file extension)? Id check but my Windows boxes are at work, I run Linux at home where I am now.Excruciation
+1 - I did some looking into this a while ago and wsf files is the only thing I found.Neologize
works in Windows Server 2003, can't believe i'm just finding this now.Burgee
E
63

You can create a (relatively) small function in each file that you want to include other files into, as follows:

sub includeFile (fSpec)
    dim fileSys, file, fileData
    set fileSys = createObject ("Scripting.FileSystemObject")
    set file = fileSys.openTextFile (fSpec)
    fileData = file.readAll ()
    file.close
    executeGlobal fileData
    set file = nothing
    set fileSys = nothing
end sub

and then use it to include specific files - these are executed as if they were inline.

includeFile "commonapi.vbi"
includeFile "dbcalls.vbi"

It basically opens the file, reads the entire contents into a string, then executes that string. There's no error handling on the I/O calls since this sort of stuff is usually done once on program start, and you want to fail if there's a problem including it.


Note that the includeFile function can be compressed to:

Sub includeFile(fSpec)
    With CreateObject("Scripting.FileSystemObject")
       executeGlobal .openTextFile(fSpec).readAll()
    End With
End Sub

Or even to (if you're not adverse to long lines):

Sub includeFile(fSpec)
    executeGlobal CreateObject("Scripting.FileSystemObject").openTextFile(fSpec).readAll()
End Sub
Excruciation answered 25/11, 2008 at 2:23 Comment(0)
G
52

The "Windows Script Host" framework (if ya want to call it that), offers an XML wrapper document that adds functionality over regular vbs files. One of which is the ability to include external script files of both the VBscript and Jscript flavors. I never got very deep into it, but I think it would do what you're wanting to do. http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx

You can include JavaScript, VBScript, or modules of other WScript script languages.

Example WSF file:

<job id="IncludeExample">
   <script language="JavaScript" src="sprintf.js"/>
   <script language="VBScript" src="logging.vbs"/>
   <script language="VBScript" src="iis-queryScriptMaps.vbs"/>
</job>

If the above file is called "iis-scriptmaps.wsf", run it this way with cscript.exe:

cscript.exe  iis-scriptmaps.wsf
Gillum answered 25/11, 2008 at 2:23 Comment(4)
Yeah, this is definitely the way to go. I used wsf files all the time in the past to include script libraries.Dionne
So can you run the wsf programs the same as the vbs ones (i.e., is there an association based on the file extension)? Id check but my Windows boxes are at work, I run Linux at home where I am now.Excruciation
+1 - I did some looking into this a while ago and wsf files is the only thing I found.Neologize
works in Windows Server 2003, can't believe i'm just finding this now.Burgee
S
22

I know this is an old thread but I post my answer anyway so others can learn what I have learnt about VBS and WSF files by "trial and error" :

So to have the same functionality as in other languages you can create one WSF file and include all of your VBS libs there, including the main program.

Something like this :

<job id="MainProg">
  <script language="VBScript" src="Constants.vbs"/>
  <script language="VBScript" src="FileFunctions.vbs"/>
  <script language="VBScript" src="SendMail.vbs"/>
  <script language="VBScript" src="LoggingFunctions.vbs"/>
  <script language="VBScript" src="MainProgram.vbs"/>   
  <script language="VBScript">
    ' Here we call the main program
    MainProgram()
  </script>
</job>

In Constants.vbs collect all constants you want to use later and in the other VBS files define your functions. In your main program file MainProgram.vbs, create a sub called MainProgram() and write your program there. In this subroutine, you can use all of the constants and functions defined in the other VBS files.

For example :

sub MainProgram()
  ' Local variables
  Dim strMessage, strSendTo, strSubject
  ' OpenFile is a function from FileFunctions.vbs
  strMessage = OpenFile("C:\Msg\message.html")
  strSendTo = "[email protected]"
  strSubject = "Daily report - " & date
  ' SendMessage is a function from SendMail.vbs
  ' cFrom and cServer are constants from Constants.vbs
  SendMessage(cFrom, strSendTo, strSubject, strMessage, cServer)
  ' Logger is a function from LoggingFunctions.vbs
  Logger("Daily report sent - " & now())
end sub

Hope you get the idea and I could help some people write better VBS apps :)

Shira answered 25/11, 2008 at 2:23 Comment(1)
The last two <script ...> elements in the WSF can be combined to: <script language="VBScript" src="MainProgram.vbs">MainProgram()</script> <!-- Here we call the main program -->.Expressman
C
6

Building on the accepted answer, here's an include sub procedure which takes a path relative to the script location instead of the working directory:

Sub include( relativeFilePath ) 
    Set fso = CreateObject("Scripting.FileSystemObject")
    thisFolder = fso.GetParentFolderName( WScript.ScriptFullName ) 
    absFilePath = fso.BuildPath( thisFolder, relativeFilePath )
    executeGlobal fso.openTextFile( absFilePath ).readAll()
End Sub

Note the you can additionally use . and .. parts in your path to include files in parent folders, etc. and it will not matter where you launch the script from. Example:

include "..\Lib\StringUtilities.vbs"
Claudio answered 25/11, 2008 at 2:23 Comment(0)
D
1

You can use the ExecuteGlobal function to run arbitrary VBS code in the global namespace. An example can be found here : http://www.source-code.biz/snippets/vbscript/5.htm

Dysentery answered 25/11, 2008 at 2:23 Comment(0)
P
1

Is this VBScript being used locally, or served classic ASP style?

If its classic ASP, you can use SSI todo it:

<!-- #include virtual="/PathTo/MyFile.vbs" -->
Pengelly answered 25/11, 2008 at 2:35 Comment(2)
It was actually a local script (under wscript or cscript) but I'm open to all suggestions. Is that loaded in the context of the whole ASP file? In other words, are the functions within MyFile.vbs available AFTER the #include?Excruciation
SSI basically just appends the files together. It happens before the script is executed.Pengelly
M
0

you can definately use the WSF script tag in cscript:

<script language="VBScript" src="ADOVBS.INC"/>

If you use ADOVBS.inc for an ADODB access make sure to remove the

<% %>

tags from ADOVBS.INC.

Mccoy answered 25/11, 2008 at 2:23 Comment(0)
C
0

IIS 5 and up also allow a script tag for including other files from an ASP file. (Is your VBScript an ASP page or a Windows script?) Here's an example:

<script language="VBScript" runat="server" src="include.asp"></script>

The behavior and rules are a bit different from server-side includes. Note: I have never actually tried using this syntax from classic ASP.

Culpepper answered 26/11, 2008 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.