Keywords: VBScript | Script Execution | WScript.Shell | Run Method | Exec Method
Abstract: This article explores various methods to execute one VBScript from another, focusing on the simple WScript.Shell.Run approach as the primary method, with supplementary techniques for advanced control and script loading. It provides code examples and practical advice to help developers choose the appropriate solution based on their needs.
Introduction
VBScript is a scripting language commonly used for automation tasks in Windows environments. A frequent requirement is to have one VBScript run another script, either to modularize code or to execute external procedures. This article discusses the core methods to achieve this, based on community best practices and documented techniques.
Method 1: Using the Run Method
The simplest and most recommended approach is to use the Run method of the WScript.Shell object. This method allows you to execute another script with minimal code. Here is a basic example:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "TestScript.vbs"
Set objShell = Nothing
In this code, an instance of WScript.Shell is created, and the Run method is called with the path to the target script. The Set objShell = Nothing line is optional but good practice for cleaning up objects. This method runs the script in a separate process, and by default, the calling script continues immediately after starting the other.
Method 2: Advanced Control with Exec Method
For scenarios requiring more control, such as monitoring the execution or passing additional parameters, the Exec method can be used. This method returns a Process object that provides access to standard input, output, and error streams, as well as the ability to terminate the process. An example is shown below:
Dim ProgramPath, WshShell, ProgramArgs, Process, ScriptEngine
Set WshShell = CreateObject("WScript.Shell")
ProgramPath = "c:\test run script.vbs"
ProgramArgs = "/hello /world"
ScriptEngine = "CScript.exe"
Set Process = WshShell.Exec(ScriptEngine & space(1) & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs)
Do While Process.Status = 0
WScript.Sleep 300
Loop
This method uses CScript.exe as the script engine and allows for waiting until the process completes. It is more complex but offers finer-grained control over the execution.
Method 3: Loading and Executing Script Content
An alternative method is to load the content of the target script and execute it within the same process using the Execute function. This can be useful for running script fragments or avoiding process overhead. Here's how it can be done:
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile("script2.vbs")
body = ts.ReadAll
ts.Close
Execute body
This approach reads the entire script file into a string and then executes it. However, it may have limitations with error handling and variable scope, so it should be used with caution.
Comparison and Best Practices
The Run method is generally preferred for its simplicity and reliability, making it the best choice for most cases where no interaction between scripts is needed. The Exec method is suitable when advanced features like process control or data exchange are required. Loading and executing script content is less common and should be reserved for specific scenarios where in-process execution is necessary.
For passing arguments, the Run method can include parameters in the command string, as shown in supplementary answers. For example: objShell.Run "TestScript.vbs 42 "an arg containing spaces" foo".
Conclusion
Executing another VBScript from within a VBScript can be efficiently achieved using the WScript.Shell.Run method for most purposes. For more complex requirements, the Exec method provides additional control. Developers should choose the method based on their specific needs, considering factors like simplicity, control, and performance.