Hun
Hun

Reputation: 69

VBscript output not writing correctly

Hello Scripting Experts,

I have a log file on remote servers.. in remote servers c:\vb\text.log

I have included my remote systems in list.Txt like

server1
server2

below is the sample of log..

application working
[10/23/2012 working

[10/24/2012 nos appdown
error found you need to check this

Below is my Script.

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

Basically the above script should search from current date line only from the text.log file that is [10/24/2012 nos appdown. Then if found as "Nos" in the current date line.. then it should write to the error.log with computer Name.

In my case the output is not coming , however looks like it is searching for the string "Nos".

Kindly gail break me from this situation....

Upvotes: 2

Views: 316

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

There are several issues with your adaptation of my script:

  • As was already pointed out by vonPryz this is the cause of the problem:

    strLine = InputFile.ReadLine
    If Left(line, Len(today)+1) = "[" & today Then

    When you change a variable name from file to strFile you have to change every occurrence of that variable, not just the line where it's assigned.

  • out.WriteLine InStr & vbTab & strComputer

    This line will also fail, because InStr is a function and you don't call it with the correct number of arguments.

  • today = Date()

    This should not be inside a loop unless you expect the date to change during the script run and need to have the current date in every loop cycle.

  • Set fso = CreateObject("Scripting.FileSystemObject")

    fso is instantiated at the beginning of the script. There's no need to re-instantiate it, especially not in each loop cycle. That's just a waste of resources.

  • Const ForReading = 1

    There's no point in defining a constant when you're never using it.

  • Do While Not ...

    Using Do Until ... would be easier to read and to understand.

Upvotes: 0

vonPryz
vonPryz

Reputation: 24091

The bug is that you don't specify the explicit option. Like so,

option explicit

This will force VBScript to complain about nondeclared variables. By doing this, you easily can spot misspelled variable names. Delcare variables with dim statement, like so

dim Fso, out

Run the script again and see that you are using a non-existing and non-initialized variable in comparision:

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

Upvotes: 3

Related Questions