【问题标题】:vbscript text file search a line with multiple stringsvbscript 文本文件搜索包含多个字符串的行
【发布时间】:2012-10-07 11:29:26
【问题描述】:

下面的vbscript需要小修改...

Const ForReading = 1

Dim strSearchFor, set1, set2
strSearchFor = "10/17/2012"
set1= app1

set2 =app2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("mylogfile.log", ForReading)

do until objTextFile.AtEndOfStream
    strLine = objTextFile.ReadLine()

    If InStr(strLine, strSearchFor and set1 or set2) <> 0 then

Wscript.Echo "we found current date with app1 or current date with app2"
    Else
        Wscript.Echo "We did not found current date"
    End If
loop
objTextFile.Close

我的实际 mylogfile.log 示例文件在文本文件中如下所示。

working on 10/17/2012 starting something ending
closing on  started app1
working on 10/17/2012 starting something app1
working on 10/17/2012 starting something app2
closing on 10/17/2012 starting something ending

提前感谢您的宝贵时间..

【问题讨论】:

    标签: scripting vbscript


    【解决方案1】:

    If InStr(strLine, strSearchFor and set1 or set2) &lt;&gt; 0 then

    这种方式是行不通的。每个Instr 调用只能搜索一个子字符串。

    If InStr(strLine, strSearchFor) &lt;&gt; 0 and (InStr(strLine, set1) or InStr(strLine, set2)) then

    另外,您的循环存在问题,您的脚本将回显您无法满足条件的每一行。使用标志或类似的东西来跟踪是否找到该行并在最后显示错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      相关资源
      最近更新 更多