Friday, June 03, 2005 3:27 AM
by
Ken
Include files and scripting? You bet - with WSF files!
In ASP we've always been able to store common routines/classes in a separate file and use an #include directive to access them. And in ASP.NET we have user controls and server controls. But what do we do with scripts that we want to run from a command line or interactively (our .vbs and .js scripts)?
A colleague at work suggested using the ExecuteGlobal function after reading in the other file using the FileSystemObject. Now this has a number of disadvantages. Firstly, I know of no way you can debug the code you're pulling in from the second file (because, in the debugger you'd just be executing a string that happens to contain 400 lines of code), and secondly you can't use classes and routines - just inline code.
Is there a better way? You bet. Beginning with Windows Scripting Host 2.0 (which shipped with Windows 2000) we can now write WSF files. WSF files have a number of benefits over straight VBS and JS files. For example, you can write both VBScript and JScript in a single WSF file (for example, if you wanted to utilise JScript's superior Try/Catch error handling, but wanted to write most of your code in VBScript). Additionally you can include other files into your main calling script, and get access to all the classes, routines, constants etc that are defined in the included files.
So, how much effort is required to port an existing VBS file to a WSF file? Probably about 30 seconds. Below is an example of a simple WSF file. It shows the necessary tags required to surround your existing code, plus an additional tag to include a second file into the main, calling file.
<job>
<script src="mainLibraryFile.vbs" language="vbscript" />
<script language="vbscript">
' Your existing VBScript code is placed here.
' And you can access any classes, routines, constants etc
' defined in mainLibraryFile.vbs
</script>
</job>
More information on WSF files can be found on the Microsoft Scripting Website and on the Scripting Guys' Blog
For the rest of June I am hoping to concentrate on IIS7, so stay tuned for IIS7 stuff!