使用mshta从txt文件内容显示弹出窗口/消息框

时间:2019-07-09 08:22:10

标签: vbscript hta

搜索有关mshta的信息,并从Internet上找到了此示例,但我不确定如何使用它来读取我的文本文件内容,然后显示txt文件内容中的msgbox

https://gist.github.com/simply-coded/4d80ce729c6cef8a860d466e8871323c

在这里

https://forums.hak5.org/topic/41985-inline-vbs-scripting/

位于C:\ List.txt的文本文件 包含

08Jan2019 : TOTAL $22.00 
14Jan2019 : TOTAL $101.00 
15Jan2019 : TOTAL $5.00 
16Jan2019 : TOTAL $12.00 
17Jan2019 : TOTAL $1.00 

如何从下面的Github中实现mshta示例,以显示包含来自txt文件的文本/字符串的msgbox?

Option Explicit
Dim wsh : Set wsh = CreateObject("Wscript.Shell")

'Message boxes that don't wait for a return to continue. No return values.
MsgBlank "tetttedghdhbcccc", "title"

'Functions for simple no wait message boxes without return values.
Function MsgBlank(m, t)
    wsh.Run "mshta.exe vbscript:Execute(MsgBox(""" & m _
        & """, vbOkOnly, """ & t & """)(window.close))"
End Function

2 个答案:

答案 0 :(得分:0)

根据您所说的,这将是您要寻找的示例:

Option Explicit
Dim wsh : Set wsh = CreateObject("Wscript.Shell")

'Message boxes that don't wait for a return to continue. No return values.
Dim fso
Dim file
Dim content
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile ("c:\List.txt", 1)
content = file.ReadAll
file.Close
MsgInformation content, "title"
'Functions for simple no wait message boxes without return values.

Function MsgInformation(m, t)
    wsh.Run "mshta.exe vbscript:Execute(MsgBox("""&m&""",vbInformation,"""&t&""")(window.close))"
End Function

我希望这对您有帮助

答案 1 :(得分:0)

实际上,在没有代码执行中断的情况下,您不需要HTA即可显示MsgBox。更重要的是,在命令行参数中传递文本或文件内容会破坏换行符。

在下面的示例中,VBScript在另一个wscript.exe进程中启动以显示MsgBox,并通过进程环境变量传递消息文本:

Option Explicit

' Check if the process launched for message output
ChkMsg

' Main code begins here
' ...
ShowMsg "Message doesn't interrupt code execution"
ShowMsg ReadTextFile("C:\List.txt", 0)
ShowMsg "Another one message"
' ...


Sub ShowMsg(sText)

    If sText = "" Then Exit Sub
    With CreateObject("WScript.Shell")
        .Environment("process").Item("msg") = sText
        .Run """" & WScript.FullName & """ """ & WScript.ScriptFullName & """", , False
        .Environment("process").Item("msg") = ""
    End With

End Sub

Sub ChkMsg()

    Dim sText

    sText = CreateObject("WScript.Shell").Environment("process").Item("msg")
    If sText = "" Then Exit Sub
    MsgBox sText
    WScript.Quit

End Sub

Function ReadTextFile(sPath, lFormat)

    ' lFormat -2 - System default, -1 - Unicode, 0 - ASCII
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 1, False, lFormat)
        ReadTextFile = ""
        If Not .AtEndOfStream Then ReadTextFile = .ReadAll
        .Close
    End With

End Function

MsgBox最多可以显示1,023个字符,如果需要更多字符,则可以使用标准记事本简单地打开文本文件内容:

sPath = "C:\List.txt"
CreateObject("WScript.Shell").Run "notepad.exe " & sPath, 0, True

顺便说一句,如果您需要更复杂的GUI(例如the examples中的图形用户界面,则最终可以求助于动态创建的HTA。