开发者

how to read text file word by word in VB script?

how to read text f开发者_运维问答ile word by word in VB script? please give all possible functions.


This:

  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

Output:

===============================================================================
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
===============================================================================

avoids all the atrocities of Zomgie's solution:

  1. Not usable with Option Explicit
  2. Useless Dim of fixed array of no size
  3. Useless array of lines
  4. Costly ReDim Preserve with extra counter
  5. Useless variable inputText
  6. Split on " " makes "First," a word


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:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜