VBScript - ForceCScript
7
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.
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






There are currently no comments for this snippet.