【问题标题】:Find the text in Vbscript在 Vbscript 中查找文本
【发布时间】:2012-10-17 15:46:16
【问题描述】:

我正在寻求帮助以读取日志文件,其中日志文件在日志中具有当前日期,我想从那里读取所有行并查找错误以输出当前日期的错误。

我的示例日志文件

[10/12/2012 testing the app1
[10/13/2012 testing the app2
[10/14/2012 testing the app3
[10/15/2012 testing the app4

我未完成的 vbscript:

Const ForReading = 1

Dim strSearchFor

strSearchFor = "currentdate in the log file"

Set objFSO = CreateObject("Scripting.FileSystemObject")

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

strLine = objTextFile.ReadLine

If log contains (inside mylogfile.log file the) current date then ' I am looking help to write the code here...

  Wscript.Echo "we found current date"

Else

  Wscript.Echo "We did not found current date"

End If

在这方面寻求帮助...非常感谢....提前...

【问题讨论】:

标签: vbscript


【解决方案1】:

InStr 将在字符串中找到子字符串

Const ForReading = 1

Dim strSearchFor
strSearchFor = "currentdate in the log file"

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

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

    If InStr(strLine, strSearchFor) <> 0 then
        Wscript.Echo "we found current date"
    Else
        Wscript.Echo "We did not found current date"
    End If
loop
objTextFile.Close

【讨论】:

  • 上述脚本可以工作...但需要进一步修改...code:///Const ForReading = 1 Dim strSearchFor, str1 strSearchFor = "日志文件中的当前日期" str1 =erro1 设置objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("mylogfile.log", ForReading) 直到 objTextFile.AtEndOfStream strLine = objTextFile.ReadLine() If InStr(strLine, strSearchFor) 0 then find str1 in日志 Else Wscript.Echo "We did not found current date" End If loop objTextFile.Close
  • 您能否更新您的问题并添加额外要求?
【解决方案2】:

这是Powershell的方式(我写的时候的问题也被标记为Powershell ...)

$log = get-content c:\path\mylogfile.log

$find = $log | select-string '10/15/2012' #the date you need to find

if ($find)
{
"we found current date"
}
Else
{
"We did not found current date"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多