Linking Scripts
What do we mean when we talk about linking scripts? Well, for starters, we have just finished talking about reusable scripts and how we can save them for future use. Another way of using these scripts is to plug them into a .wsf file and have multiple scripts execute, one after another; or you can include a script in the middle of another to perform certain tasks during the processing of the original script.
One reason that stands out as an excellent use for .wsf files and linking your scripts is the situation where you have various script files already created from previous versions of WSH, or you have script files created in different languages, such as VBScript and JScript. With the use of a .wsh file, you can include a script that was written in JScript and call a function in that script from a VBScript file. Listing 3.4 shows an example of this usage.
To see how this linking works, first create the JScript file that is shown in Listing 3.5 and save it as FSO.js. Next, create the file shown in Listing 3.4 and save it as GetDriveSpace.wsh. This will cause it to be a WSH file with a job element.
Listing 3.4 A WSH File That Contains a Code Snippet Written in VBScript and a Reference to an External JScript File That Displays the Free Drive Space for the C Drive
`********************************************************************* ` GetDriveSpace.wsh ` This .wsh file demonstrates calling a Jscript function from ` within a VBScript function and using the value returned ` from that function ` ` Author: Gerry O'Brien ` Date: April 7, 2001 ` Platform: Windows 2000/XP ` Language: VBScript/XML `********************************************************************** <job id="IncludeExample"> <script language="JScript" src="FSO.JS"/> <script language="VBScript"> ` Get the free space for drive C. Dim strSpace strSpace = GetFreeSpace("c:") WScript.Echo strSpace </script> </job>
Listing 3.5 The GetFreeSpace JScript Function Referenced in the Preceding .wsh File
//********************************************************************* // FSO.js // This script provides information regarding the path, // volume name and drive space for the drive passed in from // the GetDriveSpace.wsh file // // Author: Gerry O'Brien // Date: April 7, 2001 // Platform: Windows 2000/XP // Language: Jscript //********************************************************************* function GetFreeSpace(drvPath) { var fs, dDrive, strSpace; fs = new ActiveXObject("Scripting.FileSystemObject"); dDrive = fs.GetDrive(fs.GetDriveName(drvPath)); strSpace = "Drive " + drvPath + " - " ; strSpace += dDrive.VolumeName; strSpace += " Free Space: " + dDrive.FreeSpace/1024 + " Kbytes"; return strSpace; }
When you double-click the GetDriveSpace WSH file, the code inside the file sees that the JScript file FSO.js is to be linked to this file. This makes the function GetFreeSpace available to the WSH file, hence the VBScript procedure that calls it. Figure 3.6 shows the message box that you receive after running this script. Your drive's free space will likely differ.
Figure 3.6 A message box displaying the result of running the GetDriveSpace WSH file.
You don't have to use a WSH file to run multiple scripts. You can create the scripts you need and run those that are required one at a time by creating a batch file that contains the name of each script that you want to run. The scripts will run in the order that they are listed in the batch file. This of course defeats the purpose of a WSH file, and doesn't permit you to call procedures or functions between more than one script file. This is the power of the WSH file.
The ability to work with and manage multiple script files in this way gives you some idea as to how programmers make use of object-oriented techniques to make their lives much easier. Developers who code using OO principles create their software and components or objects with reuse in mind. This translates to time saved at a later time when they need to reuse the object in another application. By creating your script files to be reusable, you save yourself time later by eliminating the need to write the same code over again.