【问题标题】:How to get CMD output in HTA file within JavaScript如何在 JavaScript 中的 HTA 文件中获取 CMD 输出
【发布时间】:2018-10-13 10:29:34
【问题描述】:

我在我的 HTA 文件中运行一些 CMD 命令,例如

<script>
var WShell = new ActiveXObject('WScript.Shell');
WShell.run('cmd /c the_first_command');

WShell.run('cmd /c the_second_command');
</script>

第一个命令可能需要一段时间才能完全执行,例如几秒钟

只有在 CMD 输出显示上一个任务已完全完成后,我才需要运行下一个命令。

据我所知,在第一个命令之后我可以运行一个间隔

var timer = setInterval(function() {

    var cmd_output_of_the_first_command = ???;

    if(~cmd_output_of_the_first_command.indexOf('A text about the task is completed')) {
        clearInterval(timer);

        WShell.run('cmd /c the_second_command');
    }

}, 500);

那么问题是如何获取 CMD 输出?

【问题讨论】:

    标签: cmd activex jscript wsh hta


    【解决方案1】:

    好的,我找到了答案:

    var WShell = new ActiveXObject('WScript.Shell');
    var WShellExec = WShell.Exec('cmd /c the_first_command');
    
    var WShellResult = WShellExec.StdOut.ReadAll();
    if(~WShellResult.indexOf('A text about the task is completed')) {
        WShell.Run('cmd /c the_second_command');
    }
    

    任何间隔都不需要

    只是 同步执行CMD,无需查看CMD输出

    WShell.Run('cmd /c the_first_command', 0, true);
    WShell.Run('cmd /c the_second_command', 0, true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 2018-12-15
      • 1970-01-01
      相关资源
      最近更新 更多