【问题标题】:How can I send several commands to the same cmd instance from HTA file?如何从 HTA 文件向同一个 cmd 实例发送多个命令?
【发布时间】:2019-09-16 11:29:56
【问题描述】:

我在 HTA 文件中使用 WScript.Shell 启动了一个 cmd 实例。 cmd 窗口打开并准备好接收命令。如何在那里发送命令?

<html>
<head>
<script language="javascript">
var wsh = new ActiveXObject('WScript.Shell');
var cmd = wsh.Exec("cmd.exe");
function to_cmd(a_command){
    cmd.Exec(a_command);
}
</script>
<title>UI</title>
<hta:application id="app">
</head>
<body>
    <input type=button onclick="to_cmd('dir')">
</body>
</html>

是的,此代码包含错误,因为我仍然无法找到正确的方法或对象来以正确的方式执行此操作。

它可以是任何方法(不仅类似于我的方法)。主要思想是能够通过单击 HTML 按钮将不同的命令发送到同一个 cmd 窗口。

不,我不想直接向 shell 对象发送单独的命令。

【问题讨论】:

  • 我想在这里使用 wsh.Run() 而不是 Exec 会更容易..?
  • 我不确定 Run() 能否解决问题。

标签: cmd jscript hta


【解决方案1】:

您可以通过直接写入进程StdIn 来完成此操作:

// Create a shell
var shell = new ActiveXObject('WScript.Shell');

// Open the command prompt with /k flag to keep it open
var cmd = shell.exec('%comspec% /k');

// Method to write to the prompt's StdIn and redirect output to the console
// NOTE: If you want a blank console screen, remove just the ">CON" text
var runCommand = function(command) {
  cmd.StdIn.Write(command + ' >CON\n');
};

// Run a command
runCommand('echo Hello');

// Run another one after 1.5 seconds
setTimeout(function() {
  runCommand('echo World!');
}, 1500);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2011-10-05
    • 1970-01-01
    • 2022-01-05
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多