2017-02-14 124 views
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。VBS不讀取整個文本文件

我的問題是我想隱藏MsgBox中的第二行。我無法刪除它,因爲我需要它。

那麼如何隱藏line2並閱讀其他行呢?

回答

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 EOLS)

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 
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?"