Any real practical engineering development must be multiple files. But VBScript creates a multiple-file project that's a bit tricky, and it's not too good for multiple file engineering and code reuse support. In C + + engineering, we have become accustomed to using an include statement and declaring a function prototype to use functions in other modules, such a complex project can be easily decomposed into small modules to make it easier to understand and master. The situation in VBScript is somewhat different. This is related to its design goals. Initially VBScript was used in client script to support simple interactions with the client, such as simple input checking, and so on. There's obviously no way in the browser to support a call to a function in another file, and you don't know if that file exists or when it exists. So what about VBScript, which executes as a local script? Think of this command: Cscript.exe yourscript.vbs. Obviously a VBS runs in a process space (cscript process), and it has no way to get functions in another file.
Recognizing the limitations of VBScript, let's look at how to solve it.
The first need might be to run another script directly in the middle of a script run. This can be done in the following ways:
Set WshShell = CreateObject ("Wscript.Shell")
Wshshell.run "WScript c:\Test.vbs param1", True
Note that here we test.vbs run in another process space (WScript process), together with the WSH executing the current script, we have two processes altogether. This is done through Wshshell.run, and the prototype of this method is:
Object. Run (strcommand, [Intwindowstyle], [Bwaitonreturn])
If timing is important, you can specify in Bwaitonreturn this parameter whether the main script will wait for the executed script to complete before continuing.
Another point to note is the strcommand parameter, which is a complex that distinguishes each field with a space. If you need to pass the run parameter to the invoked script, you should enter it after the second space. The following example shows how to get the arguments passed by the main script. Yes, it is through wscript.arguments to visit. WScript There are some other interesting properties, please remember to read the document.
Set Oargs = wscript.arguments
For i = 0 to Oargs.count-1
WScript.Echo Oargs (i)
Next
For parametric parsing, here is an example of a script in Windows Support Tools. You can reuse this function to parse any parameters that are specified in/argname:value form.
' Searches for and returns the value of a command line argument of the form
'/argname:value from the supplied array. Erases the entry in the array so
' That's only untouched entries remain.
function Getargvalue (argname, args ())
Dim a
Dim V
Dim argnamelength
Dim x
Dim Argcount
Dim fullargname
Fullargname = "/" & Argname & ":"
Argcount = Ubound (args)
' Get the length of the ' argname we are looking for
Argnamelength = Len (fullargname)
Getargvalue = "" ' Default to Nothing
For x = 0 to Argcount
If Len (args (x)) >= Argnamelength Then
A = Mid (args (x), 1, argnamelength)
If UCase (a) = UCase (Fullargname) Then
' Erase it so we can look for unknown args later
v = args (x)
Args (x) = ""
If Len (v) > Argnamelength Then
Getargvalue = Mid (V, argnamelength + 1)
Exit function
Else
Getargvalue = ""
Exit function
End If
End If
End If
Next
End Function
More often, we need to share variables between scripts, and call functions on each other. We would like to get the convenience of C + +: With an include declaration, you can introduce functions and variables from another module into the current module. In VBScript, you can do this by Executeglobal:
Sub Include (Sinstfile)
Dim oFSO, F, S
Set oFSO = CreateObject ("Scripting.FileSystemObject")
Set f = ofso.opentextfile (Sinstfile)
s = F.readall
F.close
Executeglobal s
End Sub
In this way, add such a call to the script: Include "Mylib.vbs", you can use the global variables and functions declared in Mylib.vbs! Notice that the function here Executeglobal has a similar function execute, and if you use execute here, you won't get the effect we want. Because of the name that is exposed through execute, its scope is limited to the level at which the execute is at, here, within the function include. This is almost certainly not the result you want.
Is there any other way? Yes. VBScript's intrinsic support COM way. If you can compile your script into a COM component, you can of course invoke the method in the component in another script. Exactly, MS provides tool script Component Wizard to help us package some VBScript files into one component and provide a registration method.
Now let's learn some new ways. I mean, these methods are supported only by WSH, and you may not have met them before. WSH supports a file called *.wsf, which itself is in XML format, which allows you to assemble your VBScript scripts, as well as other types of scripts, such as Batch,perl, and put them into WSH for execution. This document is also detailed, here is not more introduction.
In this section, there are four ways to assemble a single script file into a larger project, by using these methods, you can build your own library of common functions, share variables and pass data between scripts, and so on.
Finding an implementation code on the web has played a big part in this project. With the VBS to implement many complex business, you need to put some common functions, class, etc. in a common reusable VBS file, like a library, other scripts contain the library script.
Copy Code code as follows:
Sub Include (Sinstfile)
Dim oFSO, F, S
Set oFSO = CreateObject ("Scripting.FileSystemObject")
Set f = ofso.opentextfile (Sinstfile)
s = F.readall
F.close
Executeglobal s
End Sub
When you use this, put this sub in the code and then include the following with the include "Comm.vbs".
Current 1/2 page
12 Next read the full text