Hello
I am new to scripting and have been teaching myself how to script. My goal is to create a script which searches our log files for a key word. I have pretty much achieved this but would like the results to be printed out to a text document. The problem is there are many log files on many servers and all the log files are named the same. So when writing the results out to a text document I need a meaningful name to save each of them as. Ideally I would like it to be "(computer name)(Log file name).txt" but I cant work out how to extract the computer name using my existing code. Please see script below. As I said, I am new to scripting so I apologies if it is badly written, but it does work as I need it to.
I am new to scripting and have been teaching myself how to script. My goal is to create a script which searches our log files for a key word. I have pretty much achieved this but would like the results to be printed out to a text document. The problem is there are many log files on many servers and all the log files are named the same. So when writing the results out to a text document I need a meaningful name to save each of them as. Ideally I would like it to be "(computer name)(Log file name).txt" but I cant work out how to extract the computer name using my existing code. Please see script below. As I said, I am new to scripting so I apologies if it is badly written, but it does work as I need it to.
Code:
Option Explicit
'On Error Resume Next
Dim objFSO
Dim arrServers 'Array Variables
Dim objTextFile
Dim strNextLine
Dim i
Dim TxtFile
Const ForReading = 1
Dim fNewest 'Find Newest file variables
Dim oFolder
Dim aFile
Dim strSearch 'Search Veriables
Dim ObjFile
Dim strLine
Dim SearchResult
Dim Results
'Reference Section
TxtFile = "textfile.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSearch = "error"
Set objTextFile = objFSO.OpenTextFile(TxtFile, ForReading) 'Array
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServers = Split(strNextLine , ",")
For i = 0 To Ubound(arrServers) 'Find Newest File
set oFolder = objFSO.getfolder(arrServers(i))
For Each aFile In oFolder.Files
If fNewest = "" Then
Set fNewest = aFile
Else
If fNewest.DateLastModified < aFile.DateLastModified Then
Set fNewest = aFile
End If
End If
Next
WScript.Echo(arrServers(i) & "\" & fNewest.name)
Set objFile = objFSO.OpenTextFile(arrServers(i) & "\" & fNewest.name)
strLine = objFile.ReadLine
Do Until objFile.AtEndofStream
strLine = objFile.ReadLine
SearchResult = InStr(strLine, strSearch)
If SearchResult <> 0 Then
WScript.Echo(arrServers(i).name)
End if
Loop
Wscript.Echo("")
WScript.Echo("******SEARCH COMPLETE******")
objFile.close
Next
Loop