书写线/阅读线

时间:2014-04-16 18:54:23

标签: vbscript

我试图让脚本读取文件并显示内容。但是,我需要每个窗口只显示5行。如何限制文件的显示?

Set MyFile = fso.OpenTextFile(FileName, ForReading)

Do While MyFile.AtEndOfStream <> True
    TextLine = MyFile.ReadLine 
    MsgBox TextLine,0, "Student Information" 
Loop
MyFile.Close    

解决方案:

我得到了解决方案!它来了:

Set MyFile = fso.OpenTextFile(FileName, ForReading)

Do While MyFile.AtEndOfStream <> True
        TextLine = textline & MyFile.ReadLine & VBCR
        counter = counter + 1
        if (counter mod 5 = 0) then 
        MsgBox TextLine,0, "Petar Moraliev"
        textline = ""
        end if
    Loop
    MsgBox TextLine,0, "Petar Moraliev"
    MyFile.Close

1 个答案:

答案 0 :(得分:0)

您需要一组cnLines(例如5个)元素来存储文件中的每个行范围。如果该(环)缓冲区已满,请执行输出(例如MsgBox)。最后要小心:缓冲区中是否还有行?

在代码中:

Option Explicit

Const cnLines = 5

Dim nUB  : nUB      = cnLines - 1
Dim tsIn : Set tsIn = CreateObject("Scripting.FileSystemObject").OpenTextFile(".\23117849.txt")
Dim nIdx : nIdx     = nUB
ReDim aLines(nUB)

Do Until tsIn.AtEndOfStream
   nIdx = (tsIn.Line - 1) Mod cnLines
   aLines(nIdx) = tsIn.ReadLine()
   If nIdx = nUB Then
      WScript.Echo Join(aLines, " * ") ' MsgBox Join(aLines, vbCrLf)
   End If
Loop
tsIn.Close
If Not nIdx = nUB Then
   ReDim Preserve aLines(nIdx)
   WScript.Echo Join(aLines, " * ") ' MsgBox Join(aLines, vbCrLf)
End If

输出(对于11行的文件):

cscript 23117849.vbs
1 * 2 * 3 * 4 * 5
6 * 7 * 8 * 9 * 10
11