Libraries in VBScript Code
62
The Use of Libraries in VBScript Code
Posted On: 4/28/2005
Have you ever wanted to get rid of that annoying procedure of copying and pasting standard objects and functions into your .VBS files every time you create a new script? Maybe you have a function you use in all your scripts and you do not want to have to worry about adding it each time on new script creation. The following procedure can help solve this.
All you need to do is utilize the Windows Script File (WSF) format feature of Windows Script Host (WSH) which Microsoft introduced in 1999 as part of WSH 2.0. This format will allow you to do "includes" as part of your script.
The steps are as follows:
- Open your .vbs script
- Insert the following string at the very beginning of your script to give your script an ID:
<job id="MyScript">
- Now insert the following string below the job id string to begin the vbscript code:
<script language="VBScript">
- Now go to the end of the script file and add this to close vbscript code:
</script>
- The last piece to add to complete the WSF file is the close job string:
</job>
This basically converts your vbscript file to a Windows Script File. Go ahead and test your script by running it to verify nothing broke during the modifications.
You can now add as many includes to your script as you want. These include strings go before the <script> tag and after the <job> tag. The files you want to include should also have the extension .wsf and mainly consist of your objects, functions, constants, and sub procedures. The syntax for the include statement looks like the following where SRC is equal to the path of your script containing the functions you want to include:
<script language="VBScript" src="MyScriptLib.wsf" />
The following example displays the use of a library for WScript.CreateObject
("Wscript.Shell") and Wscript.Arguments objects.
<job id="RunExe">
<script language="VBScript" src="StandardObjectsLib.wsf" />
<script language="VBScript">
Option Explicit
If args.count <> 1 Then
Wscript.echo "Incorrect number of arguments supplied"
Wscript.quit
Else
WshShell.Run args(0)
End If
</script>
</job>
And here is what the StandardObjectsLib.wsf looks like:
Dim WshShell
Set WshShell = WScript.CreateObject("Wscript.Shell")
Dim WshNet
Set WshNet = WScript.CreateObject("WScript.Network")
Dim WshFileSys
Set WshFileSys = WScript.CreateObject("Scripting.FileSystemObject")
Dim args
Set args = WScript.Arguments
|
Microsoft Windows Scripting Self-Paced Learning Guide (Pro-Other)
Price: $13.04
List Price: $44.99 |
|
Classic Shell Scripting
Price: $19.00
List Price: $34.95 |
|
Source Vehicle Scripting
Price: $19.95
List Price: $19.95 |
|
|
VBScript / Windows Scripting Host - 3 Poster Package
Price: $29.85
List Price: $59.85 |
|
5 Web Design training Courses - PHP, Dreamweaver, Frontpage, Javascript Training CDs Value Pack
Price: $29.95
|
|
Mastering Unix Shell Scripting: Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and UNIX Gurus
Price: $39.00
List Price: $50.00 |
PrintShare it! — Rate it: up down flag this hub








