Is there a way to run a command line command from JScript (not javascript) in Windows Scripting Host (WSH) cscript.exe?
Asked Answered
T

3

6

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!

Tiffanytiffi answered 17/9, 2009 at 23:32 Comment(0)
C
9

You can execute DOS commands using the WshShell.Run method:

var oShell = WScript.CreateObject("WScript.Shell");
oShell.Run("timeout /t 10", 1 /* SW_SHOWNORMAL */, true /* bWaitOnReturn */);


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):

var oShell = WScript.CreateObject("WScript.Shell");
oShell.Popup("Click OK to continue.", 10);

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 the ENTER key press (not any key). Anyway, here's an example, just in case:

WScript.Echo("Press the ENTER key to continue...");

while (! WScript.StdIn.AtEndOfLine) {
   WScript.StdIn.Read(1);
}
Caren answered 18/9, 2009 at 6:31 Comment(0)
T
1

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:

var oShell = WScript.CreateObject("WScript.Shell");
oShell.SendKeys("cls{enter}timeout /t 10{enter}");

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

Tiffanytiffi answered 18/9, 2009 at 7:30 Comment(3)
Your SendKeys string is incorrect - it should be "cls{enter}timeout /t 10{enter}" (without pluses).Caren
Also, your variant is very unreliable. First of all, SendKeys sends keystrokes to the active window, so the input might be sent to another window that has been accidentally activated. Also, if there're commands following SendKeys, they won't nesessary be executed after SendKeys. Try inserting the WScript.Echo commands before and after SendKeys and removing cls from the input string -- and you will see that the script output is messed up and the timeout command is executed last of all.Caren
Yeah..this code is really messy! I started experiencing problems once I ran it with the rest of the code and its true that with SendKeys, statements don't necessarily get executed as they appear. It gets even worse if you have a WScript.StdIn.Readline() with SendKeys(), as it wouldn't wait for the user to input coz it's already getting input from SendKeys and it does break up things the nasty way! Lesson learnt: Be very careful with SendKeys, it can be sloppy great insight helen!Tiffanytiffi
A
0

Yes, with the WScript.Shell object.

See the docs and samples

Assertive answered 18/9, 2009 at 0:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.