0
    If objFSO.GetFile(tempFolder & "\list2.txt").Size > 0 Then 
        contents = objFSO.OpenTextFile(tempFolder & "\list2.txt", 1, False).ReadAll
 Select Case  MsgBox ("Link" & contents & "",vbYesNoCancel+vbExclamation+vbSystemModal,"Wassup?")

这是我的vbs代码。我有 list2.txt 并且在 min3 max8 行之间有多行。如您所见,我在 MsgBox 中显示 list.txt。

我的问题是我想在 MsgBox 中隐藏第二行。我不能删除它,因为我需要它。

那么如何隐藏 line2 并阅读其他行呢?

4

3 回答 3

0
Option Explicit 

' Prepare a test buffer
Dim contents
    contents = Join( Array("line 1", "line 2", "line 3", "line 4"), vbCrLf )
    WScript.Echo contents

' Option 1 - Remove second line via regular expression
Dim purgedContents
    With New RegExp
        .Pattern = "[\r\n]+[^\r\n]*"
        purgedContents = .Replace( contents, "" )
    End With 
    WScript.Echo "--------------------------"
    WScript.Echo purgedContents

' Option 2 - Array operation
Dim i
    purgedContents = Split( contents, vbCrLf )
    For i = 1 To UBound( purgedContents ) - 1
        purgedContents(i) = purgedContents( i+1 )
    Next 
    Redim Preserve purgedContents( UBound(purgedContents) - 1 )
    purgedContents = Join( purgedContents, vbCrLf )
    WScript.Echo "--------------------------"
    WScript.Echo purgedContents

' Option 3 - Array 2 - If we don't need to keep empty lines
    purgedContents = Split( contents, vbCrLf )
    purgedContents(1) = ""
    purgedContents = Replace( Join( purgedContents, vbCrLf ), vbCrLf+vbCrLf, vbCrLf )
    WScript.Echo "--------------------------"
    WScript.Echo purgedContents
于 2017-02-14T17:02:19.727 回答
0

像这样的东西怎么样:

Dim i
i = 1

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim oFile
Set oFile = objFSO.OpenTextFile(tempFolder & "\list2.txt", 1, False)

Dim contents
If objFSO.GetFile(tempFolder & "\list2.txt").Size > 0 Then
    Do
        ' Collect data for all lines except line 2
        ' -----------------------------------------
        If i <> 2 Then
            contents  = contents & vbNewLine & oFile.ReadLine
        Else
            ' Skip un required line
            ' ----------------------
            oFile.ReadLine
        End If

        i = i + 1
    Loop Until oFile.AtEndOfStream
End If

MsgBox "Link" & contents & "",vbYesNoCancel+vbExclamation+vbSystemModal,"Wassup?"
于 2017-02-15T03:51:36.360 回答
0

您不能在 MegBox 中隐藏部分文本。您需要一份没有第 2 行的输入文件内容的副本。使用RegExp

Option Explicit

' a string of 5 lines
Dim s : s = Replace("one two three four five", " ", vbCrLf)
' a RegExp to delete line 2
Dim r : Set r = New RegExp
r.Pattern = "(.+)(\n.+)(\n[\s\S]+)"
' a copy of s without line 2
Dim t : t = r.Replace(s, "$1$3")
WScript.Echo Replace(s, vbCrLf, "\r\n")
WScript.Echo Replace(t, vbCrLf, "\r\n")
WScript.Echo t
MsgBox t

输出:

cscript 42231154.vbs
one\r\ntwo\r\nthree\r\nfour\r\nfive
one\r\nthree\r\nfour\r\nfive
one
three
four
five

(我假设 vbCrLf EOL)

于 2017-02-14T16:49:07.857 回答