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.
我正在编写一个在cscript.exe中运行的JScript程序。是否可以从脚本中运行commnad line命令。这真的会让工作变得简单,因为我可以运行某些命令而不是在jscript中编写更多代码来执行相同的操作。
For example: In order to wait for a keypress for 10 seconds, I could straight away use the timeout command
例如:为了等待按键10秒,我可以立即使用超时命令
timeout /t 10
Implementing this in jscript means more work. btw, I'm on Vista and WSH v5.7
在jscript中实现这意味着更多的工作。顺便说一下,我在Vista和WSH v5.7上
any ideas? thanx!
有任何想法吗?感谢名单!
3 个解决方案
#1
8
You can execute DOS commands using the WshShell.Run
method:
您可以使用WshShell.Run方法执行DOS命令:
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):
如果您特别需要暂停脚本执行,直到按下某个键或超时,则可以使用WshShell.Popup方法(带有超时选项的对话框)完成此操作:
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.
但是,此方法在cscript下运行时也会显示一个消息框。
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.StdIn属性直接从输入流中读取,这种方式等待输入。但是,从输入流中读取不支持超时,仅在按下ENTER键时返回(不是任何键)。无论如何,这是一个例子,以防万一:
WScript.Echo("Press the ENTER key to continue...");
while (! WScript.StdIn.AtEndOfLine) {
WScript.StdIn.Read(1);
}
#2
1
thanx for the help ppl, this was my first post and * is awesome! Also, I figured out another way to do this thing, using the oShell.SendKeys()
method.
Here's How:
thanx for help ppl,这是我的第一篇文章,*很棒!另外,我想出了另一种使用oShell.SendKeys()方法做这件事的方法。就是这样:
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
这样,您几乎可以运行每个dos命令,而不会生成新的进程或窗口
EDIT: Although it seems to solve the problem, this code is not very reliable. See the comments below
编辑:虽然它似乎解决了这个问题,但这段代码并不是很可靠。请参阅以下评论
#1
8
You can execute DOS commands using the WshShell.Run
method:
您可以使用WshShell.Run方法执行DOS命令:
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):
如果您特别需要暂停脚本执行,直到按下某个键或超时,则可以使用WshShell.Popup方法(带有超时选项的对话框)完成此操作:
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.
但是,此方法在cscript下运行时也会显示一个消息框。
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.StdIn属性直接从输入流中读取,这种方式等待输入。但是,从输入流中读取不支持超时,仅在按下ENTER键时返回(不是任何键)。无论如何,这是一个例子,以防万一:
WScript.Echo("Press the ENTER key to continue...");
while (! WScript.StdIn.AtEndOfLine) {
WScript.StdIn.Read(1);
}
#2
1
thanx for the help ppl, this was my first post and * is awesome! Also, I figured out another way to do this thing, using the oShell.SendKeys()
method.
Here's How:
thanx for help ppl,这是我的第一篇文章,*很棒!另外,我想出了另一种使用oShell.SendKeys()方法做这件事的方法。就是这样:
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
这样,您几乎可以运行每个dos命令,而不会生成新的进程或窗口
EDIT: Although it seems to solve the problem, this code is not very reliable. See the comments below
编辑:虽然它似乎解决了这个问题,但这段代码并不是很可靠。请参阅以下评论