Scripting VBscript输出未正确写入

Scripting VBscript输出未正确写入,scripting,vbscript,Scripting,Vbscript,大家好,脚本专家 我在远程服务器上有一个日志文件。。 在远程服务器中c:\vb\text.log 我已将我的远程系统包括在list.Txtlike中 server1 server2 下面是日志的示例 application working [10/23/2012 working [10/24/2012 nos appdown error found you need to check this 下面是我的脚本 Set Fso = CreateObject("Scripting.FileSy

大家好,脚本专家

我在远程服务器上有一个日志文件。。 在远程服务器中
c:\vb\text.log

我已将我的远程系统包括在
list.Txt
like中

server1
server2
下面是日志的示例

application working
[10/23/2012 working

[10/24/2012 nos appdown
error found you need to check this
下面是我的脚本

Set Fso = CreateObject("Scripting.FileSystemObject")
Set InFile = fso.OpenTextFile("list.Txt")
Set out = fso.CreateTextFile("error.log")

Const ForReading = 1
Do While Not (InFile.atEndOfStream)
  strComputer = InFile.ReadLine
  today = Date()
  Set fso = CreateObject("Scripting.FileSystemObject")
  strFilePath = "\\" & strComputer & "\c$\vb\"

  Set InputFile = fso.OpenTextFile(strFilePath & "text.log", 1)
  Do While Not (InputFile.AtEndOfStream)
    strLine = InputFile.ReadLine

    If Left(line, Len(today)+1) = "[" & today Then
      ' line timestamped with today's date
      If InStr(line, "nos") > 0 Then
        ' line contains "error"
        out.WriteLine InStr & vbTab & strComputer
      End If
    End If
  Loop
  InputFile.close
Loop

out.Close
InFile.Close
基本上,上面的脚本应该只从
文本.log
文件(即
[10/24/2012 nos appdown
)中从当前日期行搜索。如果在当前日期行中找到“nos”,那么它应该使用计算机名写入
错误.log

在我的例子中,输出没有出现,但是看起来它正在搜索字符串“Nos”


请将我从这种情况中打断……

错误在于您没有指定显式选项。就像这样

option explicit
这将迫使VBScript抱怨未声明的变量。通过这样做,您很容易发现拼写错误的变量名。Delcare变量与dim语句类似

dim Fso, out
再次运行脚本,查看您在比较中使用的是不存在且未初始化的变量:

strLine = InputFile.ReadLine ' Read stuff to strLine
If Left(line, Len(today)+1) = "[" & today Then ' ERROR. line has no value!

您对我的脚本的改编有几个问题:

  • 正如已经指出的,这是问题的原因:

    strLine=InputFile.ReadLine

    如果左(行,Len(今天)+1)=“[”&今天,则

    当您将变量名从
    文件
    更改为
    strFile
    时,您必须更改该变量的每一次出现,而不仅仅是指定该变量的行

  • out.writelineinstr&vbTab&strComputer

    这一行也将失败,因为它是一个函数,并且您没有使用正确数量的参数调用它

  • today=Date()

    这不应该在循环内,除非您希望在脚本运行期间更改日期,并且需要在每个循环周期中都有当前日期

  • Set fso=CreateObject(“Scripting.FileSystemObject”)

    fso
    在脚本开始时实例化。无需重新实例化,尤其是在每个循环周期中。这只是浪费资源

  • Const ForReading=1

    如果你从来不使用常数,那么定义它就没有意义了

  • 做而不做…

    使用
    Do-Until…
    更容易阅读和理解

可能的副本。