Libraries in VBScript Code

62
rate or flag this page

By rodtrent



The Use of Libraries in VBScript Code

By: Jason Scheffelmaer

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


Print   —   Rate it:  up  down  flag this hub

RSS for comments on this Hub

No comments yet.

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working