如何在JavaScript中的HTA文件中获取CMD输出

时间:2018-10-13 10:29:34

标签: cmd activex jscript wsh hta

我在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输出?

1 个答案:

答案 0 :(得分:0)

好,我找到了答案:

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');
}

任何时间都不需要

OR

正义 无需检查CMD输出,就可以一对一地同步执行CMD

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