从远程系统读取的文本文件

时间:2012-10-19 09:19:55

标签: scripting vbscript

嗨脚本专家......

在下面的代码中寻找你的帮助.... RemoteServersList.Txt和我想调用每个系统,如\ RemoteServerName \ E $ \ V2 \ log.Txt我缺少几行代码......

请帮助我...... 非常感谢....

Set Fso = CreateObject("Scripting.FileSystemObject")

Set InFile = fso.OpenTextFile("RemoteServersList.Txt")

Const ForReading = 1

Do While Not (InFile.atEndOfStream)

strComputer = InFile.ReadLine


today = Date()

Set fso = CreateObject("Scripting.FileSystemObject")


strFilePath = "\\" & strComputer & "\E$\V2\" ' This is wrong 'here I am missing a 'code.. looking for your help

Set InputFile = fso.OpenTextFile("strFilePath\log.txt", 1)  ' This is wrong here I am 'missing a code.. looking for your help


Set out = fso.OpenTextFile("error.log", 2)

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

    WScript.Echo "Hello" ' I need to append this to output file
    out.WriteLine line & vbTab & InputFile.ReadLine & vbTab & strComputer


            If InStr(line, "error") > 0 Then
        ' line contains "error"
        out.WriteLine line & vbTab & InputFile.ReadLine & vbTab & strComputer

        End If
    End If


    Loop

InputFile.Close
out.Close


Loop

1 个答案:

答案 0 :(得分:1)

RemoteServerList.txt是一个txt文件,其中包含一个服务器列表,如下所示:

server-1
server-2
server-3
etc.

并且在每台服务器上都必须阅读此文本文件:

\\server-1\e$\V2\log.txt
\\server-2\e$\V2\log.txt

我在代码中看到的第一个问题是在字符串赋值中,这应该是正确的:

strFilePath = "\\" & strComputer & "\E$\V2\"
Set InputFile = fso.OpenTextFile(strFilePath & "log.txt", 1)

但你想在哪里写error.log文件? localy还是在远程机器上?

如果你想在本地机器上使用它,最好只在脚本开头只打开文件一次,就在Set Inifile下面。

如果文件尚不存在,您必须使用 CreateTextFile 或使用 fso.OpenTextFile(“error.log”,2,True)打开它:< / p>

Set Fso = CreateObject("Scripting.FileSystemObject")
Set InFile = fso.OpenTextFile("RemoteServersList.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 & "\E$\V2\"

  Set InputFile = fso.OpenTextFile(strFilePath & "log.txt", 1)
  Do Until InputFile.AtEndOfStream

    If (condition) Then
      out.WriteLine [write something to output file]
    End If

  Loop
  InputFile.close

Loop

out.Close
InFile.Close