有没有办法在 Windows Scripting Host (WSH) cscript.exe 中从 JScript(不是 javascript)运行命令行命令?
我正在编写一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
WshShell.Run
方法:如果您特别需要暂停脚本执行,直到按下某个键或超时,您可以使用
WshShell.Popup
方法(带有超时选项的对话框):但是,此方法在 cscript 下运行时也会显示消息框。
本文介绍了另一种可能的方法:如何暂停脚本然后当用户按下键盘上的某个键时恢复它? 简而言之,您可以使用
WScript.StdIn
属性直接从输入流读取并以这种方式等待输入。但是,从输入流读取不支持超时,并且仅在按下ENTER
键(而不是任何键)时返回。无论如何,这是一个例子,以防万一: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:感谢 ppl 的帮助,这是我的第一篇文章,stackoverflow 太棒了!
另外,我找到了另一种方法来完成此操作,即使用
oShell.SendKeys()
方法。方法如下:
通过这种方式,您可以运行几乎每个 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:
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
是的,使用
WScript.Shell
对象。查看文档和示例
Yes, with the
WScript.Shell
object.See the docs and samples