Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a vbscript from another vbscript

Tags:

vbscript

How do I get a vbscript to run another vbscript?

Id imagine its only a few lines of code but not tried doing this before, nothing is passed between the 2, one just needs to call/run the other.

For examples the script being run is called TestScript.vbs, the other script for it to call/run would be called Secondscript.vbs, both of which are located in C:\Temp.

Thanks Mark

like image 764
markdigi Avatar asked Nov 06 '09 09:11

markdigi


People also ask

Can you run VBScript in CMD?

If you want to execute a VBScript in the command prompt, you can use cscript.exe. See an example below. You can use Wscript. Echo in the VBScript in order to print information that will be displayed in the command prompt.


2 Answers

See if the following works

Dim objShell Set objShell = Wscript.CreateObject("WScript.Shell")  objShell.Run "TestScript.vbs"   ' Using Set is mandatory Set objShell = Nothing 
like image 92
Martin Avatar answered Oct 20 '22 23:10

Martin


You can try using the Wshshell.Run method which gives you little control of the process you start with it. Or you could use the WshShell.Exec method which will give you control to terminate it, get a response, pass more parameters (other than commandline args), get status, and others

To use Run (Simple Method)

Dim ProgramPath, WshShell, ProgramArgs, WaitOnReturn,intWindowStyle
Set WshShell=CreateObject ("WScript.Shell")
ProgramPath="c:\test run script.vbs"
ProgramArgs="/hello /world"
intWindowStyle=1
WaitOnReturn=True
WshShell.Run Chr (34) & ProgramPath & Chr (34) & Space (1) & ProgramArgs,intWindowStyle, WaitOnReturn

ProgramPath is the full path to your script you want to run
ProgramArgs is the arguments you want to pass to the script. (NOTE: the arguments are separated by a space, if you want to use an argument that contains a space then you will have to enclose that argument in quotes [Safe way to do this is use CHR (34) Example ProgramArgs= chr (34) & "/Hello World" & chr (34)])
IntWindowStyle is the integer that determines how the window will be displayed. More info on this and WaitOnReturn can be found here WshShell.Run Method
WaitOnReturn if true then the script will pause until the command has terminated, if false then the script will continue right after starting command.

NOTE: The Run method can return the exit code but you must set WaitOnReturn to True, and assign the 'WshShell.Run' to a variable. (EX: ExitCode=WshShell.Run (Command,intWindowStyle,True))

To Use EXEC (Advanced Method)

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
    'Currently Waiting on the program to finish execution.
    WScript.Sleep 300
Loop

ProgramPath same as Run READ RUN'S DESCRIPTION
ProgramArgs DITTO
ScriptEngine The Engine you will be using for executing the script. since the exec method requires a win32 application, you need to specify this. Usually either "WScript.exe" or "CScript.exe". Note that in order to use stdin and stdout (we'll cover what these are a bit further down) you must choose "CScript.exe".
Process this is the Object that references to the program the script will start. It has several members and they are: ExitCode, ProcessID, Status, StdErr, StdIn, StdOut, Terminate.

More Details about the members of Process Object

  1. ExitCode This is the exit code that is returned when the process terminates.
  2. ProcessID This is the ID that is assigned to the process, every process has an unique processID.
  3. Status This is a code number that indicates the status of the process, it get set to '-1' when the process terminates.
  4. StdErr This is the object that represents the Standard Error Stream
  5. StdIn This is the Object that represents the Standard Input Stream, use it to write additional parameters or anything you want to pass to the script you are calling. (Process.StdIn.WriteLine "Hello Other Worlds")
  6. StdOut This is the Object that represents the Standard Output Stream, It is READONLY so you can use Process.StdOut.ReadLine. This is the stream that the called script will receive any information sent by the calling script's stdin. If you used the stdin's example then StdOut.Readline will return "Hello Other Worlds". If there is nothing to read then the script will hang while waiting for an output. meaning the script will appear to be Not Responding
    Note: you can use Read or ReadAll instead of ReadLine if you want. Use Read (X) if you want to read X amount of characters. Or ReadAll if you want the rest of the stream.
  7. Terminate Call this method to force terminate the process.

For more information about WshShell.Exec go to Exec Method Windows Scripting Host

like image 44
Ronnie Matthews Avatar answered Oct 20 '22 23:10

Ronnie Matthews