Have you ever wanted to include another .vbs file with all your common functions in a script?
Many people I have met thought it wasn’t possible.
But it is actually pretty easy to do with “ExecuteGlobal”
to make it even easier, I have created a small function which you can put in your scripts and use a simple
Include(“C:\functions.vbs”)
I have made 2 small example scripts to show you how to use it:
Functions.vbs:
Sub WriteLog(strMessage) Const FOR_APPENDING = 8 strFileName = "C:\test.txt" Set objFS = CreateObject("Scripting.FileSystemObject") If objFS.FileExists(strFileName) Then Set oFile = objFS.OpenTextFile(strFileName, FOR_APPENDING) Else Set oFile = objFS.CreateTextFile(strFileName) End If oFile.WriteLine strMessage End Sub
This file could be a shared file with all your common functions such as Log writer.
Include-Example.Vbs
Include("C:\functions.vbs") WriteLog "Start of Script!" Sub Include (strFile) 'Create objects for opening text file Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile(strFile, 1) 'Execute content of file. ExecuteGlobal objTextFile.ReadAll 'CLose file objTextFile.Close 'Clean up Set objFSO = Nothing Set objTextFile = Nothing End Sub
Then everytime you create a script you include the function and make sure to include the other vbs files in the beggning of the script.
Now you have access to all the functions from the other scripts, from inside your new script!
and the next time the management tells you to change the Log Format to a different date/time format, so simular. You just have to change the function in the shared file, instead of editing all your scripts!
But beware not to corrupt the shared file, cause it will make all your scripts fail 😉
Merry x-mas!
Hi Jacob,
why don’t you use build-in feature of .wsf files?
There’s no problem using .vbs include files:
include.vbs:
Sub Test
sText = “Huhu”
End Sub
sample.wsf:
Call Test ‘ you can treat subs or functions from include.vbs as if they exist in sampe.wsf
MsgBox sText
~msenn
Sorry, your php removed the Job and Script Tags!
I have posted this on another forum but may be useful here as well :
“Hi all,
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 :
‘ Here we call the main program
MainProgram()
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 sub 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:Msgmessage.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 🙂 “
Thanks designed for sharing such a fastidious thinking, post is good, thats
why i have read it fully
Wow! Who knew?
I have used WSF files, but they are another, clunky layer to keep track of.
Expanding on what was presented: I tried adding multiple “include” statements at the start of the main VBS file and then tried a “sideways” call from a subroutine in one included file to a subroutine in another.
It all worked. Woo-hoo!
I stored all the included files in a “libs” subdirectory of the main VBS file — Include(“libs\functions.vbs”)
Also worked — when the main VBS file is run from the “working directory”.
The tried “dim” a variable before the “Include” statements and printing it from two subs, including the “sideway” call.
Again, it all worked as desired.
The messiness of the WSF technique has hindered my efforts to write and reuse library files.
This technique allows me to see dependencies within the main VBS file without having to look at a WSF file.
How did I not know about this post for eleven years?!
Thank you for sharing.