gpt4 book ai didi

VBScript - 从 stdout 捕获输出

转载 作者:行者123 更新时间:2023-12-03 22:23:52 25 4
gpt4 key购买 nike

我知道这已在另一个问题中得到回答,但我只是不明白它是如何完成的。

我正在尝试将命令行程序(Aria2 下载器)的输出放入 HTA 脚本中,以便对其进行解析,并获取下载百分比、文件大小等并动态更新到 DIV 中。

这是我已经调整并尝试使用的代码,但它只是锁定界面,直到命令行完成,然后显示所有输出,而不是在它通过时显示它。

Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

Do While WshShellExec.Status = WshRunning
window.setTimeOut "", 100
Loop

Select Case WshShellExec.Status
Case WshFinished
strOutput = WshShellExec.StdOut.ReadAll()
Case WshFailed
strOutput = WshShellExec.StdErr.ReadAll()
End Select

Set objItem = Document.GetElementByID("status")
objItem.InnerHTML = "" & strOutput & ""

我该如何修改它,使其不会锁定我的用户界面并抓取输出并将其显示在“状态”div 中?

最佳答案

问题是你的代码没有结束,把控制权还给了浏览器。在程序结束之前,您不会离开循环,并且感知到的状态是接口(interface)挂起,直到子进程结束。

您需要设置回调,以便浏览器会定期调用您的代码,您将在其中更新状态并离开。

<html>
<head>
<title>pingTest</title>
<HTA:APPLICATION
APPLICATIONNAME="pingTest"
ID="pingTest"
VERSION="1.0"
/>
</head>

<script language="VBScript">
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2

Dim WshShellExec, Interval

Sub Window_onLoad
LaunchProcess
End Sub

Sub LaunchProcess
Set WshShellExec = CreateObject("WScript.Shell").Exec("ping -n 10 127.0.0.1")
Interval = window.setInterval(GetRef("UpdateStatus"),500)
End Sub

Sub UpdateStatus
Dim status
Set status = Document.GetElementByID("status")
Select Case WshShellExec.Status
Case WshRunning
status.InnerHTML = status.InnerHTML & "<br>" & WshShellExec.StdOut.ReadLine()
Case WshFinished, WshFailed
status.InnerHTML = status.InnerHTML & "<br>" & Replace(WshShellExec.StdOut.ReadAll(),vbCRLF,"<br>")
window.clearInterval(Interval)
Interval = Empty
End Select
End Sub
</script>

<body>
<div id="status"></div>
</body>
</html>

关于VBScript - 从 stdout 捕获输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32920690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com