如何在VB脚本中逐字阅读文本文件?

时间:2011-03-21 11:16:53

标签: vbscript

如何在VB脚本中逐字逐句阅读文本文件?请提供所有可能的功能。

2 个答案:

答案 0 :(得分:1)

此:

  Dim sAll : sAll    = readAllFromFile("..\data\wbw.txt")
  WScript.Echo sAll
  WScript.Echo "-----------------------"

  Dim oRE  : Set oRE = New RegExp
  oRE.Global  = True
  oRE.Pattern = "\w+"
  Dim oMTS : Set oMTS = oRE.Execute(sAll)
  Dim oMT
  For Each oMT In oMTS
      WScript.Echo oMT.Value
  Next

输出:

===============================================================================
How to read text file word by word in VB script?
Please give all possible functions.
First, read the file line-by-line into an array. Then, when you're reading
through the array, parse each line word-by-word. As such:

-----------------------
How
to
read
text
file
word
by
word
in
VB
script
Please
give
all
possible
functions
First
read
the
file
line
by
line
into
an
array
Then
when
you
re
reading
through
the
array
parse
each
line
word
by
word
As
such
===============================================================================

避免了Zomgie解决方案的所有暴行:

  1. 不适用于Option Explicit
  2. 没用大小的固定数组的无用Dim
  3. 无用的行数
  4. 昂贵的ReDim Preserve with extra counter
  5. 无用的变量inputText
  6. 拆分“”使“第一个”,一个字

答案 1 :(得分:0)

首先,逐行读取文件到数组中。然后,当您阅读数组时,逐字逐句地解析每一行。就这样:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\file.txt", ForReading)

Const ForReading = 1

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
    Redim Preserve arrFileLines(i)
    arrFileLines(i) = objFile.ReadLine
    i = i + 1
Loop
objFile.Close

For Each strLine in arrFileLines

    inputText = strLine

    outputArray = Split(inputText)

    For Each x in outputArray
        WScript.Echo "Word: " & x
    Next
Next
相关问题