VBScript - ForceWScript





9
Date Submitted Tue. Nov. 28th, 2006 4:04 AM
Revision 1 of 1
Helper TimboTheGreat
Tags VBSCRIPT | wscript
Comments 0 comments
This script demonstrates the user of a procedure that forces the currently executing script to execute under the WScript scripting host. This forces the output from WScript.Echo statements to be displayed message box dialog. This functionality may be useful in situations where the default scripting host is set to cscript.exe and/or the programmer uses a common set of code to program for wscript.exe and cscript.exe.

Typical usage would see the ForceWScript procedure called at the beginning of a script.

ForceWScript
Main

Sub Main
        WScript.Echo "This script is running in the WScript script host"
End Sub

Sub ForceWScript()
        'Declare local variables
        Dim strHost
        Dim strCmdLine
        Dim objShell
        Dim objFSO
       
        '
Determine the current script host
        strHost = Right(WScript.FullName, 11)
        strHost = LCase(strHost)
       
        'Check if we need to restart ourselves
        If strHost <> "wscript.exe" Then
                '
Create objects
                Set objShell = CreateObject("WScript.Shell")
                Set objFSO = CreateObject("Scripting.FileSystemObject")
               
                'Build the command line
               
                '
Add the desired scripting host
                strCmdLine = Chr(34) & objFSO.BuildPath(WScript.Path, "wscript.exe") & Chr(34)
                'Add a dividing space
                strCmdLine = strCmdLine & " "
                '
Add the path of the currently executing script
                strCmdLine = strCmdLine & Chr(34) & WScript.ScriptFullName & Chr(34)
               
                'Run the script
                objShell.Run strCmdLine
               
                '
Destroy objects
                Set objShell = Nothing
                Set objFSO = Nothing

                'Quit the script
                WScript.Quit
        End If
End Sub
 

Tim M

Comments

There are currently no comments for this snippet.

Voting