有没有办法在 Windows Scripting Host (WSH) cscript.exe 中从 JScript(不是 javascript)运行命令行命令?

发布于 2024-08-04 15:28:30 字数 276 浏览 11 评论 0原文

我正在编写一个在 cscript.exe 中运行的 JScript 程序。是否可以从脚本内运行命令行命令。这确实会让工作变得简单,因为我可以运行某些命令,而不是在 jscript 中编写更多代码来完成同样的事情。


例如: 为了等待按键 10 秒,我可以立即使用超时命令,

timeout /t 10

在 jscript 中实现这个命令意味着更多的工作。 顺便说一句,我对 Vista 和 WSH v5.7

有什么想法吗?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随遇而安 2024-08-11 15:28:30

您可以使用 WshShell.Run方法:

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

如果您特别需要暂停脚本执行,直到按下某个键或超时,您可以使用 WshShell.Popup 方法(带有超时选项的对话框):

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

但是,此方法在 cscript 下运行时也会显示消息框。

本文介绍了另一种可能的方法:如何暂停脚本然后当用户按下键盘上的某个键时恢复它? 简而言之,您可以使用 WScript.StdIn 属性直接从输入流读取并以这种方式等待输入。但是,从输入流读取不支持超时,并且仅在按下 ENTER 键(而不是任何键)时返回。无论如何,这是一个例子,以防万一:

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

while (! WScript.StdIn.AtEndOfLine) {
   WScript.StdIn.Read(1);
}

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);
}
一笔一画续写前缘 2024-08-11 15:28:30

感谢 ppl 的帮助,这是我的第一篇文章,stackoverflow 太棒了!
另外,我找到了另一种方法来完成此操作,即使用 oShell.SendKeys() 方法。
方法如下:

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

通过这种方式,您可以运行几乎每个 dos 命令,而无需生成新进程或窗口

编辑:虽然它似乎解决了问题,但此代码不是很可靠。请参阅下面的评论

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

爱她像谁 2024-08-11 15:28:30

是的,使用 WScript.Shell 对象。

查看文档和示例

Yes, with the WScript.Shell object.

See the docs and samples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文