|
<< Click to Display Table of Contents >> Navigation: Programming Mechworks PDM > DBWCommandShell > Programmer's Guide > Use directly the WSH in a script of DBWorks |
DBWorks can host scripts written for the Microsoft VBScript 5.1 engine; sometimes may be useful, however, to be able to use some features of the Microsoft Windows Scripting Host engine.
Suppose, for example, that you want to use the capability of the Windows Scripting Host to include some scripts into some other so to have a better organization of your routines. Another advantage of this technique is that you can also use scripts written in JScript ( Javascript ), if some programmer, for some reasons, has more ability with this language.
The trick is that you must have a .VBS script matched with a .WSF file, that can support the WSH features.
Look at the following example ( it is assumed DBWorks installed in the default directory):
Sub main()
Set shell = CreateObject( "WScript.Shell" )
shell.Run("wscript ""C:\program files\dbworks\lst\test_ws.wsf""")
End Sub
<Job id="UseOfWindowsScriptingHostExample">
<script language="JScript" src="C:\program files\dbworks\FSO.JS"/>
<script language="VBScript" src="C:\program files\dbworks\dbwscrpt.lib"/>
<script language="VBScript" src="C:\program files\dbworks\dbwaddin.lib"/>
<script language="VBScript">
option explicit
Dim s
'Get free space for drive C
s = GetFreeSpace("c:")
Wscript.Echo s
call DBWShell("OpenForBrowsing")
if (okDBW = FALSE) then stop
</script>
</Job>
function GetFreeSpace(drvPath) {
var fs, d, s;
fs = new ActiveXObject(""Scripting.FileSystemObject);
d = fs.GetDrive(fs.GetDriveName(drvPath))
s = "Drive " + drvPath + " - ";
s += d.VolumeName;
s += "Free Space: " + d.FreeSpace/1024 + " KBytes";
return s;
}
Copy the three files in the LST directory of DBWorks. If you now go in the DBWorks->Utility menu you will see the TEST.VBS script; when launching it, pratically you will transfer the control to the WSCRIPT process ( with the shell.Run command ), that will start the script written specifically for the WSH engine.
Please note the .WSF file extension for this type of script.
Note in the TEST.WSF script the second, third and fourth lines, that includes some scripts, loading them from files.
The first one shows how to include functions written in JScript language into a VBScript script.
The second and third includes are MANDATORY for any script application running from within DBWorks.
After that, it comes the true code of the script; note, for example, the instruction option explicit, available only in WSH. Note also the absence of the subroutine Main(), not needed here.