Launch One Script From Another

January 14th, 2008

Can you call a WSH script from within an existing VBS WSH logon script?- Cheryl J.

Hello, Cheryl. Quite simply, yes you can. There are a couple of methods for doing this. I’ll show you the easiest two first and then I’ll document a third, more robust way, in a future article.

To do this, you can rely on the WScript Object Model. It provides us with two methods designed specifically for calling external scripts or applications.

The first is the Run method made available by the WshShell object. It requires a command line and accepts the following syntax:

object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

Object is a required reference to the WshShell object. The Run method accepts three parameters.

The first is a required command line string. This must be a valid Windows command line.

The second is an optional integer value that represents the program’s window style. Not all programs will use this. Acceptable values are listed in the table below.

intWindowStyle Description
0 Hides the window and activates another window.
1 Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2 Activates the window and displays it as a minimized window.
3 Activates the window and displays it as a maximized window.
4 Displays a window in its most recent size and position. The active window remains active.
5 Activates the window and displays it in its current size and position.
6 Minimizes the specified window and activates the next top-level window in the Z order.
7 Displays the window as a minimized window. The active window remains active.
8 Displays the window in its current state. The active window remains active.
9 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.

The third is a boolean value the indicates whether or not you script should wait for execution to complete before continuing to the next statement. If omitted, this defaults to false.

Keeping this in mind, you can easily launch one script from within another by using the command line. You’ll need to decide between using cscript.exe and wscript.exe and you’ll need the full path to the script you wish to launch.

Set WshShell = CreateObject("WScript.Shell")
strScriptPath = "C:\temp\myOtherScript.vbs"
 
WshShell.Run "cscript.exe " & Chr(34) & strScriptPath & Chr(34), 0, vbTrue

Notice how I’ve used Chr(34) on either side of my path. This returns a quotation mark ensuring that I don’t run into any problems if my script path contains any spaces. The actual command line being used looks like this:

cscript.exe “C:\temp\myOtherScript.vbs”

You can also add any command line switches that you require. Any valid command line will work. This is essentially the same as executing a command line from the Run dialog box on your start menu.

Hope this answers your question!

On a side note, the WshShell object also has a Exec method for executing command lines. It has less flexibility but will allow you to interact with the newly launched script when used correctly.

[donotprint] [/donotprint] Please use the trackback link when linking to this post.

Related Posts:

Add to Technorati Favorites

6 Responses to “Launch One Script From Another”

  1. Mike Says:

    Hey, great tutorial.. Thanks!

  2. Stephen Says:

    Hey,

    You don’t need to use the chr(34) for the quotation marks. doubling up quotation marks causes a single quotation mark to appear in a string. Note: This can be a triple quotation mark if the quotation mark comes at the beginning of the string or a quadruple quotation mark if only a single quotation mark is the only thing in a string.

    Examples

    string = “He really “”killed”" at that.”
    wscript.echo string
    ‘ this will output: He really “killed” at that.

    string = “he was a real “”monster”"”
    wscript.echo string
    ‘ this will output: he was a real “monster”

    string = “”"”
    wscript.echo string
    ‘ this will output: ”

    so
    WshShell.Run “cscript.exe ” & Chr(34) & strScriptPath & Chr(34), 0, vbTrue
    becomes:
    WshShell.Run “cscript.exe “”" & strScriptPath & “”"”, 0, vbTrue

    This is a far less cumbersome (if slightly more initially confusing) method of getting quotation marks into strings.

  3. Nilpo Says:

    Hi Stephen,

    Thanks for the comment. I’m going to have to politely disagree with you that doubling quotation marks is less cumbersome. It becomes very hard to differentiate string values that way. Since most other programming languages do not allow this, it’s that much more cumbersome to seasoned programmers.

    The correct way to insert a double quote in string is to use the Chr() function.

    That being said, you’re certainly free to use whichever method you prefer. That’s one of the nice things about VBScript.

  4. Anonymous Says:

    .~: I am really thankful to this topic because it really gives up to date information “*:

  5. Arun Says:

    Hi Nilpo,

    Say, dada.vbs calls mama.vbs and I am running dada.vbs in a command window using cscript. Problem is that echo messages of the mama.vbs is not appearing, but dada.vbs displays its echo messages.

    I need both to display in the same cmd window. How can I achieve this ?

    Thanks in advance,

    Arun

  6. Nilpo Says:

    Hi, Arun. To learn how you can do that, you should read my article over at ASP Free.
    http://www.aspfree.com/c/a/windows-scripting/error-trapping-and-capturing-third-party-output-in-wsh/

Leave a Reply

.htaccess Apache article articles by Nilpo ASP ASP Free automation clipboard copy desktop Dev Shed docx Internet Explorer message box Microsoft PHP registry remove script scripting text tutorial tweak tweets Twitter UAC updates User Account Control VBS VBScript vista volume Windows Windows 7 Windows Guru Windows Script Windows Script Host Windows Scripting Windows Vista Windows XP Word WScript wscript.exe WSH XP