Reputation: 93348
I maintain a good number of vbscripts for performing various startup scripts on my network and have a handful of functions that I use in almost all of them.
Short of copy and paste, does anyone have suggestions for how I can go about creating libraries of reusable vbscript code. I'm not averse to using a code generator for doing so as long as it isn't more of a headache than copy and paste is. But any recommendations would be appreciated.
Thanks
Upvotes: 2
Views: 1321
Reputation: 7769
I use a synthetic include
(as below) and store my libraries in a subdirectory.
Sub Include( scriptName )
Dim sScript
Dim oStream
With CreateObject( "Scripting.FileSystemobject" )
Set oStream = .OpenTextFile( scriptName )
End With
sScript = oStream.ReadAll()
oStream.Close
ExecuteGlobal sScript
End Sub
Obviously, if there's an error in the included script, you're not going to get a very helpful error message (VBScript won't be able to tell you what line the problem occurred on.) Once everything works, however ...
Include "c:\script\stdlib.vbs"
Include "c:\script\INI.cls"
Upvotes: 3
Reputation: 338228
VBScript has the Execute
statement, so if you don't want to go the WSF route Tester101 proposes, you can do an include like this:
Set fso = CreateObject("Scripting.FileSystemObject")
file = "your_library.vbs"
Execute fso.OpenTextFile(file, 1).ReadAll
Set foo = New FooClass
MsgBox foo.GetBar()
Assuming "your_library.vbs"
would contain a class definition for FooClass
.
Be sure to call Execute in global context, or you will get into scoping issues.
Of course be sure to have all your files under tight control to prevent malicious usage.
Upvotes: 6