I'm writing a JScript program which is run in cscript.exe. Is it possible to run a commnad line command from within the script. It would really make the job easy, as I can run certain commands instead of writing more code in jscript to do the same thing.
For example: In order to wait for a keypress for 10 seconds, I could straight away use the timeout command
timeout /t 10
Implementing this in jscript means more work. btw, I'm on Vista and WSH v5.7
any ideas? thanx!
You can execute DOS commands using the
WshShell.Run
method:If you specifically need to pause the script execution until a key is pressed or a timeout elapsed, you could accomplish this using the
WshShell.Popup
method (a dialog box with a timeout option):However, this method displays a message box when running under cscript as well.
Another possible approach is described in this article: How Can I Pause a Script and Then Resume It When a User Presses a Key on the Keyboard? In short, you can use the
WScript.StdIn
property to read directly from input stream and this way wait for input. However, reading from the input stream doesn't support timeout and only returns upon theENTER
key press (not any key). Anyway, here's an example, just in case:thanx for the help ppl, this was my first post and stackoverflow is awesome! Also, I figured out another way to do this thing, using the
oShell.SendKeys()
method.Here's How:
This way you can run almost every dos command without spawning a new process or window
EDIT: Although it seems to solve the problem, this code is not very reliable. See the comments below
Yes, with the
WScript.Shell
object.See the docs and samples