In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "vbscript how to achieve include", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "vbscript how to achieve include" this article.
Any real practical engineering development is bound to be multi-file. But VBScript is a bit more troublesome to create a multi-file project, and its support for multi-file engineering and code reuse is not very good. In the project, we are used to using functions in other modules through an include statement and declaring a function prototype. Such a complex project can be easily decomposed into some small modules to be understood and mastered more easily. Things are a little different in VBScript. This has something to do with its design goals. VBScript was originally used in client-side scripting to support simple interaction with the client, such as simple input checking, and so on. There is obviously no way to support calls to functions in another file in the browser. You don't know if and when that file exists. What about VBScript that is executed as a local script? Think about 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 may be to run one script directly in the middle of another. This can be done in the following ways:
Set WSHShell = CreateObject ("WScript.Shell")
WSHShell.Run "wscript c:\ Test.vbs param1", True
Notice here that our Test.vbs runs in another process space (wscript process), plus the WSH that executes 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 the bWaitOnReturn parameter whether the master script will wait for the execution of the script to finish before continuing.
Another thing to note is the strCommand parameter, which is a complex that distinguishes fields by spaces. If you need to pass the run parameter to the called script, you should enter it after the second space. The following example shows how to get the parameters passed by the master script. By the way, it is accessed through WScript.Arguments. WSCript has some other interesting properties, so please remember to read the documentation.
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 / 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 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 to each other. We want to get the same convenience as in C _ Include +: functions and variables from another module can be introduced into the current module through a Cramp declaration. In VBScript, you can do this through 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 a call to the script: Include "mylib.vbs", and you can use the global variables and functions declared in mylib.vbs! Notice that the function ExecuteGlobal here has a similar function Execute. If we use Execute here, we won't get the effect we want. Because the name exposed through Execute is limited to the level at which Execute is located, in this case, within the function Include. This is almost certainly not what you want.
Is there any other way? Right. VBScript inherently supports the COM approach. If you can compile your script into a COM component, of course you can call the methods in the component in other scripts. As it happens, MS provides the tool Script Component Wizard to help us package some VBScript files into a component and provide a way to register.
Now let's learn some new methods. I mean, these methods are only supported by WSH, and you may not have encountered them before. WSH supports a file called * .wsf, which itself is in XML format, through which you can assemble your VBScript scripts, as well as other types of scripts, such as batch,perl, and give them to WSH for execution. The documentation in this area is quite detailed, so I won't introduce it here.
This section introduces four ways to assemble a larger project from a single script file. By using these methods, you can build your own library of common functions, share variables and transfer data between scripts, and so on.
Finding a piece of code for implementation on the Internet has played a big role in this project. When using vbs to implement many complex businesses, you need to write some common functions, class, etc., in a common reusable vbs file, just like a library, other scripts contain scripts for this library.
The copy 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, put the sub in the code, and then use Include "comm.vbs" to include the following
The above is all the content of the article "how to achieve include in vbscript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.