Sounds like you want to
spawn a WSH script from within an HTML page.
Warning: Free (worth the price) opinions ahead. ;-)
There are multiple reasons
not to do this, almost all having to do with security, and others having to do with inconvenience. The latter is mostly due to the multiple security alerts your users should be getting when pages like this load and when they attempt actions that will create "hazardous" objects like WSHShell.
The cleanest thing to do may be to create your page as an HTA, and for the most part this is as easy as naming the file xxx.hta instead of xxx.htm. Unless the script runs a long time, you'd probably be better off just putting all of the grunt work in the HTA and forego running WSH at all.
There are times when you want special facilities of WSH though, some as mundane as the .Sleep() method.
Lets just go ahead with the plan you had.
Here is an example WSH script.
test.vbs
Code:
Option Explicit
Dim FSO, Args, tsOut
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Args = WScript.Arguments
Set tsOut = FSO.CreateTextFile(Args(0) & "test.txt", True)
tsOut.WriteLine CStr(Now)
tsOut.Close
Set tsOut = Nothing
Set Args = Nothing
Set FSO = Nothing
'Detect script host used, notify the user.
If InStr(1, WScript.FullName, "cscript.exe", vbTextCompare) > 0 Then
WScript.StdOut.WriteLine _
WScript.ScriptName & " done, please press enter."
WScript.StdIn.ReadLine
Else
MsgBox "Done.", vbOkOnly, WScript.ScriptName
End If
This script wants one command-line parameter, the path to create a small test.txt file in. It is written to work via either CScript or WScript. If you don't need the pause at the end you can cut that stuff right out.
Here is an HTML page from which the WSH script can be kicked off using either CScript or WScript.
text.htm
Code:
<html>
<head>
<script language=vbscript>
Option Explicit
Dim WSHShell
Dim Path
Sub SetPath()
Dim PathParts, X
PathParts = Split(Mid(window.location.pathname, 2), "%")
For X = 1 To UBound(PathParts)
PathParts(X) = _
Chr(CInt("&H" & Left(PathParts(X), 2))) _
& Mid(PathParts(X), 3)
Next
Path = Join(PathParts, "")
Path = Left(Path, InStrRev(Path, "\"))
End Sub
Sub btnCScript_onclick()
'Run: cscript "{path}test.vbs" "{path}"
WSHShell.Run _
"cscript """ & Path & "test.vbs"" """ & Path & """"
window.status = "Script has been run via CScript"
End Sub
Sub btnWScript_onclick()
'Run: wscript "{path}test.vbs" "{path}"
WSHShell.Run _
"wscript """ & Path & "test.vbs"" """ & Path & """"
window.status = "Script has been run via WScript"
End Sub
Sub window_onload()
Set WSHShell = CreateObject("WScript.Shell")
SetPath
End Sub
Sub window_onunload()
Set WSHShell = Nothing
End Sub
</script>
</head>
<body>
<input type=button id=btnCScript value="Use CScript">
<input type=button id=btnWScript value="Use WScript">
</body>
</html>
To keep things neat, I have it all in one folder. This includes the test.htm, the test.vbs, and even the test.txt that is created by test.vbs when it runs.
That is accomplished by having the HTML page fetch and decode its full path and source file and trim it back to just the path. This path is used both to run the WSH script
and passed to the WSH script as an argument so that the WSH script knows where I want it to create test.txt for me. In your application you may not need to fuss with the path this way.
Why people mess about with running a command shell, wrapped around CScript, wrapped around their scripts sometimes escapes me. Once again there are good reasons to do this at times, but it usually isn't necessary.
If you think my code above is "full of quotes" it gets even worse if you run cmd.exe and have to feed it lots of stuff, like a complex command line. This may be where your attempt failed: not enough quotes.
Also, in most cases it is perfectly fine
and even superior to use WScript instead of CScript to run small user-initiated utility scripts. If your WSH script doesn't do any user interaction, you never see WScript running anyway. If it does interact, using CScript you usually end up clunking around to try to keep the window open like I do in this example - or else invoke a command shell as you were doing above. The clunk-o-rama is usually needed because if the WSH script fails on some error, the "pause" I coded in mine above won't help. The console window just closes after a blink. Using WScript results in a nice error dialog you can read and then "ok."
That may be what was happening to yours.
The WScript MsgBox() and InputBox() functions are perfectly good for user interaction. If you need richer interaction you are probably better off writing the whole thing as an HTA in the first place.