VBScript - ForceCScript





7
Date Submitted Tue. Nov. 28th, 2006 3:59 AM
Revision 1 of 1
Helper TimboTheGreat
Tags BATCH | cscript | VBSCRIPT
Comments 0 comments
This script demonstrates the user of a procedure that forces the currently executing script to execute under the CScript scripting host. This allows the output from WScript.Echo statements to be displayed in a command prompt dialog in a similar way to older style batch files, making this procedure ideal for batch processing.

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

ForceCScript
Main

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

Sub ForceCScript()
        '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 <> "cscript.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, "cscript.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