搜索文本文件

时间:2016-06-18 09:28:50

标签: string vbscript

我正在尝试阅读文本文件并计算文本文件中出现短语/字符串(而不是单词)的次数,但到目前为止我所拥有的是:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("D:\VBscript project\testing.txt", ForReading)
strContents = objFile.ReadAll
objFile.Close

i = 0

arrLines = Split(strContents, "")

For Each strLine in arrLines
    If InStr(strLine, "hi there") Then
        i = i + 1
    End If
Next

WScript.Echo "Number of times word occurs: " & i

这只允许我计算单词出现的次数,当我尝试将其调整为计数短语时,这不起作用。

4 个答案:

答案 0 :(得分:4)

考虑以下示例:

strPath = "D:\VBscript project\testing.txt"
strPhrase = "hi there"

strContent = ReadTextFile(strPath, 0)
arrContent = Split(strContent, strPhrase)

MsgBox "Number of times phrase occurs: " & UBound(arrContent)

Function ReadTextFile(strPath, lngFormat)
    ' lngFormat -2 - System default, -1 - Unicode, 0 - ASCII
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 1, False, lngFormat)
        ReadTextFile = ""
        If Not .AtEndOfStream Then ReadTextFile = .ReadAll
        .Close
    End With
End Function

请注意,基于Split的方法区分大小写。

答案 1 :(得分:2)

如果我理解正确并且您要求的内容非常简单,您可以将"hi there"字符串更改为参数。通过这种方式,您可以动态地告诉您的函数要查找的内容。

编辑:感谢@omegastripes,我注意到我之前的代码中存在一个缺陷,所以这是一个可行的。

代码如下:

Sub yourSubName (pstrTextToCount)
    Const ForReading = 1
    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objFile : Set objFile = objFSO.OpenTextFile("D:\VBscript project\testing.txt", ForReading)
    Dim strContents : strContents = objFile.ReadAll
    objFile.Close

    ' You don't need these objects anymore, so release them
    Set objFile = Nothing
    Set objFSO = Nothing

    Dim intTextPosition : intTextPosition = 0
    Dim i : i = -1
    Do
      i = i + 1
      intTextPosition = InStr(intTextPosition + 1, strContents, pstrTextToCount)
    Loop While (intTextPosition > 0)

    Wscript.Echo "Number of times '" & pstrTextToCount & "' occurs: " & i
End Sub

我假设您的Sub只会这样做,这就是我将其括在SubEnd Sub语句中的原因。您可以添加所需的任何其他编码,但只记得在Sub的签名上添加所需的参数,以使其正常工作。

PS:作为一种良好做法,始终Dim您的变量并释放Set objName = Nothing不再需要的对象的内存

答案 2 :(得分:2)

strPath = "D:\VBscript project\testing.txt"
strPhrase = "hi there"

strContent = ReadTextFile(strPath, 0)
arrContent = Split(strContent, strPhrase)

MsgBox "Number of times phrase occurs: " & UBound(arrContent)

Function ReadTextFile(strPath, lngFormat)
    ' lngFormat -2 - System default, -1 - Unicode, 0 - ASCII
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 1, False, lngFormat)
        ReadTextFile = ""
        If Not .AtEndOfStream Then ReadTextFile = .ReadAll
        .Close
    End With
End Function

答案 3 :(得分:1)

这是一个使用正则表达式的版本,因此您可以指定搜索是否需要区分大小写。 对于testpurpose,我使用脚本本身的内容作为输入。

Dim path, phrase, content
path    = Wscript.ScriptFullName
phrase  = "hi there\^$*+?{}.()|[]"
content = CreateObject("Scripting.FileSystemObject").OpenTextFile(path).ReadAll

Function NumberOfPhrasesInString(phrase, text, IgnoreCase)
  Dim regexpr, matches
  Set regexpr = New RegExp
  phrase = RegExEscape(phrase)
  With regexpr
    .Pattern = phrase
    .Global  = True
    .IgnoreCase = IgnoreCase
    Set matches = .Execute(text)
  End With
  NumberOfPhrasesInString = matches.count
End Function

Function RegExEscape(str)
  Dim special
  RegExEscape = str
  special = "\^$*+?{.()|[]"
  For i=1 To Len(special)
    RegExEscape = replace(RegExEscape, Mid(special, i, 1), "\" & Mid(special, i, 1))
  Next
End Function

Wscript.Echo "Number of times phrase occurs: " & NumberOfPhrasesInString(phrase, content, false)

作为奖励,因为我在这里也转换到Ruby那个版本

path    = __FILE__ # the path to this script for test purposes
phrase  = 'HI THERE \ ^ $ * + ? { . ( | ['
puts phrase
content = File.read path

def number_of_phrases_in_string(phrase, text, ignoreCase=false )
  escaped = Regexp.escape(phrase)
  text.scan(Regexp.new(escaped, ignoreCase)).count.to_s 
end

puts "Number of times phrase occurs: " + number_of_phrases_in_string(phrase, content, true)

或单行

puts File.read(__FILE__).scan(Regexp.new(Regexp.escape(phrase), true)).count

最后一行中的true定义了casesensitivity