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



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

最新更新