Implementation of VBScript include on page 1/2

Source: Internet
Author: User

Any practical project development must be multi-file. However, creating a multi-File Project in VBScript requires a little trouble, Code The support for reuse is not very good. In the C/C ++ project, we are used to using functions in other modules by using an Include statement and declaring the original form of a function, such a complex project can be easily divided into small modules for easier understanding and control. VBScript has different situations. This is related to its design goals. VBScript was originally used in client scripts to support simple interaction with the client, such as simple input check. Obviously, there is no way in the browser to support function calls in another file. You do not know whether the file exists or when it exists. So what about the VBScript executed in the form of a local script? Think about this command: cscript.exe yourscript. vbs. Obviously, a vbs runs in a process space (cscript process), and it cannot obtain functions in another file.

Recognizing the limitations of VBScript, let's look at how to solve it.

The first requirement may be that another script needs to be directly run in the middle of a script. This can be done through the following method:

Set wshshell = Createobject ("wscript. Shell ")
Wshshell. Run "wscript c: \ test. vbs param1", true

Note that test. vbs runs in another process space (wscript process), and wsh executes the current script. We have two processes in total. This is done through wshshell. Run. The prototype of this method is:

Object. Run (strcommand, [intwindowstyle], [bwaitonreturn])

If the time sequence is important, you can specify in the bwaitonreturn parameter whether the master script can continue until the executed script stops running.

Another thing to note is the strcommand parameter, which is a complex and distinguishes fields with spaces. If you need to pass the running parameters to the called script, enter the second space. The following example shows how to obtain the parameters passed by the main script. By the way, it is accessed through wscript. arguments. Wscript has other interesting attributes. Please read the document.

Set oargs = wscript. Arguments
For I = 0 to oargs. Count-1
Wscript. Echo oargs (I)
Next

For Parameter Parsing, here is an example of a script in Windows 2000 support tools. You can reuse this function to parse any parameters specified in the form of/argname: value.

'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 only untouched entries remain.

Function getargvalue (argname, argS ())
Dim
Dim v
Dim argnamelength
Dim X
Dim argcount
Dim fullargname

Fullargname = "/" & argname &":"
Argcount = ubound (ARGs)

'Get the length of the argname we are looking
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 and call functions between scripts. We want to get the convenience in C/C ++: With an include declaration, we can introduce functions and variables in another module into the current module. In VBScript, you can use executeglobal to implement:

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, you can use the global variables and functions declared in mylib. vbs by adding such a call: Include "mylib. vbs" in the script! Note that the executeglobal function has a similar Execute function. If execute is used here, it will not achieve the desired effect. Because the name exposed through execute is limited to the execute level, here, that is, inside the include function. This is almost certainly not the result you want.

Are there other methods? Yes. VBScript supports the com method. If you can compile your script into a COM component, you can certainly call the methods in the component in other scripts. Exactly, Ms provides the script component wizard tool to help us pack some VBScript files into a component and provide the registration method.

Now let's learn some new methods. I mean, these methods are only supported by wsh, and you may not have met them before. Wsh supports *. WSF file, which is in XML format, can be used to assemble your VBScript and other types of scripts, such as batch and Perl, run the command in wsh. This document is still detailed. I will not introduce it here.

This section describes four methods for assembling a single script file into a large project. By using these methods, you can create your own common function libraries, share variables and transmit data among various scripts.
Finding a piece of implementation code on the Internet has played a major role in this project. When using vbs to implement many complex services, you need to write some public functions and classes in a public reusable vbs file, like a library, other scripts include the scripts of this library.Copy codeThe Code is 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 using this sub, put it in the code, and then use include "Comm. vbs" to include the following:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.