【问题标题】:Display text file using MsgBox使用 MsgBox 显示文本文件
【发布时间】:2017-05-25 01:39:01
【问题描述】:

我有多个文件名:

c123we_1014_A1_45333 c123we_1014_A2_45333 c123we_1014_A3_45333

我需要的是只获取第三个参数并使用消息框显示它。 我所做的是获取第三个参数并将其写入文本文件。

For Each filelog In FileList
    LogFile = Split(filelog, "~")(1)

    Set otf = fso.OpenTextFile("C:\Data\" & LogFile, 1)

    sFile = Split(LogFile, "_")
    CurStep = sFile(3)

    FileNameStep = LotId & "_" & "Step"
    ScriptPath = Mid(ScriptFolder, 1, Len(ScriptFolder) - 8)

    If Not fso.FileExists(ScriptFolder & "\" & FileNameStep & ".txt") Then
        Set ctf = fso.CreateTextFile(ScriptFolder & "\" & FileNameStep & ".txt", True)
        ctf.Close
    End If

    Set otf = fso.OpenTextFile(ScriptFolder & "\" & FileNameStep & ".txt", 8)
    otf.Writeline "Current - " & CurStep
    otf.Close
Next

我的文本文件输出如下:

当前 - A1 电流 - A2 电流 - A3

我不知道如何使用消息框显示文本文件的内容。

我也尝试过使用数组而不是将其写入 txt 文件,这比使用 txt 文件更简单。我的代码如下:

For Each filelog In FileList
    LogFile = Split(filelog, "~")(1)

    Set otf = fso.OpenTextFile("C:\Data\" & LogFile, 1)

    l = 0
    MsgBox FileList.Count
    Do While l < FileList.Count 
        sFile = Split(LogFile, "_")
        CurStep = sFile(4)

        array.Add CurStep 

        l = l + 1
    Loop
Next

MsgBox Join(array, vbNewLine)

但出现错误。错误在MsgBox Join() 行:

错误:无效的过程调用或参数

【问题讨论】:

    标签: arrays vbscript msgbox


    【解决方案1】:

    将数据写入文本文件后,您可以将其关闭,然后按照以下步骤操作:

    A.再次以读取模式打开文件并为其设置对象引用:

    Set objFile = fso.OpenTextFile(ScriptFolder & "\" & FileNameStep & ".txt",1)
    

    B.使用 readall() 方法读取文件的内容并将其存储在变量中:

    tempData = objFile.readAll()
    

    C.关闭文件并使用'Msgbox'显示内容

    objFile.Close
    MsgBox tempData
    

    如果要在文本文件中逐行显示数据,可以使用 readline() 方法并将步骤 B 修改为:

    While not fso.atEndOfStream
        tempData = fso.readline()
        Msgbox tempData
    Wend
    

    编辑 2:对于问题的第二部分:

    您不应该使用单词“array”作为变量名,因为它是 vbscript 中的关键字。此外,您不要使用 .Add 在数组中添加元素,因为我们在这里讨论的是数组而不是数组列表。

    您可以使用以下代码替换您的代码:

    Dim intCtr: intCtr=-1
    Dim tempArr()
    For Each filelog in FileList    
    LogFile = Split(filelog, "~")(1)
    
    Set otf = fso.OpenTextFile("C:\Data\" & LogFile, 1)
    
         l = 0
        msgbox FileList.Count
        do while l < FileList.Count 
            intCtr=intCtr+1
            sFile = Split(LogFile, "_")
            CurStep = sFile(4)
            Redim preserve tempArr(intCtr)
            tempArr(intCtr)=CurStep 
            l = l + 1
        Loop
    next
    
    MsgBox Join(tempArr, vbNewLine)
    

    【讨论】:

    • 谢谢。这段代码工作得很好。其实我还有一个问题,如何在 msgbox 中显示数组?
    • MsgBox Join(array, vbNewLine)
    • 我之前已经尝试过了,但出现了错误。错误是“无效的过程调用或参数”
    • 为此,我们需要查看数组的内容。我在您发布的代码中看不到任何数组。
    • @Kira 我试过你的代码,它的工作但输出加倍。输出 A1 A1 A2 A2 A3 A3 。它应该只有A1 A2 A3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多