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

How to get CMD output in HTA file within JavaScript

我 运行 我的 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输出?

好的,我找到答案了:

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