scripting - VBscript 输出写不正确

标签 scripting vbscript

脚本专家您好,

我在远程服务器上有一个日志文件..
在远程服务器中 c:\vb\text.log
我已将我的远程系统包含在 list.Txt 中喜欢

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

基本上,上面的脚本应该只从 text.log 从当前日期行开始搜索文件是 [10/24/2012 nos appdown .然后,如果在当前日期行中发现为“Nos”.. 那么它应该写入 error.log与计算机名称。

在我的情况下,输出没有出现,但看起来它正在搜索字符串“Nos”。

请盖尔让我摆脱这种情况......

最佳答案

错误是您没有指定显式选项。像这样,

option explicit

这将迫使 VBScript 提示未声明的变量。通过这样做,您可以轻松地发现拼写错误的变量名称。带有dim语句的Delcare变量,像这样
dim Fso, out

再次运行脚本,看到您使用的是不存在且未初始化的变量进行比较:
strLine = InputFile.ReadLine ' Read stuff to strLine
If Left(line, Len(today)+1) = "[" & today Then ' ERROR. line has no value!

关于scripting - VBscript 输出写不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13041988/

相关文章:

linux - 在 su 命令期间调用时如何隐藏密码?

shell - Ubuntu shell - 消除一行的条件

linux - 我可以在 bash 中设置特定数字而不是序列吗?

vbscript - 如何在 VBScript 脚本中断言?

linux - 在 Shell 脚本 Linux 上获取一元运算符错误

linux - bash 脚本中的 z${variable} 和 zfalse 是什么意思?

c# - 安装项目的自动增量版本

parameters - 如何将参数从 Batch (.bat) 传递到 VBScript (.vbs)?

batch-file - 如何在VBS Shell命令参数中转义等号=

javascript - SCRIPT1005 : Expected '(' Error in user. ASP