Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help parsing text in multiple locations 1

Status
Not open for further replies.
Mar 3, 2011
12
0
0
US
I need some help parsing text files located in multiple locations. Example:
\\server1\backup\mon\log.txt
\\server2\backup\mon\log.txt
\\server3\backup\mon\log.txt

My goal is to setup a task that runs nightly to parse all log files in the Mon folder on each server and return any failed session found. The same process would run everyday of the week Mon, Tue, Wed etc. I do have a scripe that works but only if I point it to a single file. Your help/comments would be greatly appriciated. See code below.


Code:


Const ForReading = 1
Const TriStateUseDefault = -2

strFile = "\\server1\Backups\log.txt"
arrKeyWords = Array("Failed")

strEmailFrom = "BackupAlert@mydomain.com"
strEmailTo = "me@mydomain.com"
strEmailSubject = "lines extracted from " & strFile
strSMTP = "smtp.mydomain.com"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
arrLines = Split(objFile.ReadAll, vbCrLf)
objFile.Close

For Each strLine in arrLines
For Each strKeyWord in arrKeyWords
If InStr(1, strLine, strKeyWord, vbTextCompare) > 0 Then
strEmailBody = strEmailBody & strLine & vbCrLf & vbCrLf
Exit For
End If
Next
Next

If strEmailBody <> "" Then
Set objEmail = CreateObject("CDO.Message")
objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = strEmailSubject
objEmail.Textbody = strEmailBody
objEmail.Configuration.Fields.Item _
(" = 2
objEmail.Configuration.Fields.Item _
(" = strSMTP
objEmail.Configuration.Fields.Item _
(" = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End If
 
[red]1. Define a list of backup servers.[/red]
[blue]2. Discover the day.[/blue]
[green]3. Wrap the code in a for loop that traverses each backup server.[/green]
[purple]4. Use the day to build the backup path.[/purple]

Code:
Const ForReading = 1
Const TriStateUseDefault = -2 

arrKeyWords = Array("Failed") 
[red]arrServers = array("Server1", "server2", "Server3")[/red]

strEmailFrom = "BackupAlert@mydomain.com"
strEmailTo = "me@mydomain.com"
strEmailSubject = "lines extracted from " & strFile
strSMTP = "smtp.mydomain.com" 

Set objFSO = CreateObject("Scripting.FileSystemObject")

[blue]
strDay = weekday(date)
select case (strDay)
	case 1 : strDay = "sun"
	case 2 : strDay = "mon"
	case 3 : strDay = "tues"
	case 4 : strDay = "wed"
	case 5 : strDay = "thurs"
	case 6 : strDay = "fri"
	case 7 : strDay = "sat"
end select
[/blue]

[green]
for i = 0 to ubound(arrServers)
	[purple]strFile = "\\" & arrServers(i) & "\backup\" & strDay & "\log.txt"[/purple]
	set objStream = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
	do while not objStream.AtEndOfFile
		strLine = objStream.ReadLine
		[black]For Each strKeyWord in arrKeyWords        
			If InStr(1, strLine, strKeyWord, vbTextCompare) > 0 Then            
				strEmailBody = strEmailBody & strLine & vbCrLf & vbCrLf            
				Exit For        
			End If    
		Next[/black]
	loop
next
[/green]

If strEmailBody <> "" Then    
	Set objEmail = CreateObject("CDO.Message")    
	objEmail.From = strEmailFrom    
	objEmail.To = strEmailTo    
	objEmail.Subject = strEmailSubject    
	objEmail.Textbody = strEmailBody    
	objEmail.Configuration.Fields.Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2    
	objEmail.Configuration.Fields.Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = strSMTP    
	objEmail.Configuration.Fields.Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25    
	objEmail.Configuration.Fields.Update    
	objEmail.Send
End If

-Geates




"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thanks for your prompt response. I get an error:
line:29
Char:5
Error:Object doesn't support this property or method:
'objStream.AtEndOfFile'
Code:800A01B6
Source: Microsoft VBScript runtime error



Const ForReading = 1
Const TriStateUseDefault = -2

arrKeyWords = Array("Failed")
arrServers = arrayarrServers = array("Server1", "server2", "Server3")

strEmailFrom = "BackupAlert@mydomain.com"
strEmailTo = "me@mydomain.com"
strEmailSubject = "lines extracted from " & strFile
strSMTP = "smtp.mydomain.com"


Set objFSO = CreateObject("Scripting.FileSystemObject")


strDay = weekday(date)
select case (strDay)
case 1 : strDay = "sun"
case 2 : strDay = "mon"
case 3 : strDay = "tue"
case 4 : strDay = "wed"
case 5 : strDay = "thu"
case 6 : strDay = "fri"
case 7 : strDay = "sat"
end select

for i = 0 to ubound(arrServers)
strFile = "\\" & arrServers(i) & "\backups\" & strDay & "\log.txt"
set objStream = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
do while not objStream.AtEndOfFile
strLine = objStream.ReadLine
For Each strKeyWord in arrKeyWords
If InStr(1, strLine, strKeyWord, vbTextCompare) > 0 Then
strEmailBody = strEmailBody & strLine & vbCrLf & vbCrLf
Exit For
End If
Next
loop
next


If strEmailBody <> "" Then
Set objEmail = CreateObject("CDO.Message")
objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = strEmailSubject
objEmail.Textbody = strEmailBody
objEmail.Configuration.Fields.Item(" = 2
objEmail.Configuration.Fields.Item(" = strSMTP
objEmail.Configuration.Fields.Item(" = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End If
 
oops. .AtEndOfStream not .AtEndOfFile. Also, this code is simply conceptual (untested ).

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Made those changes but now I do no recieve an email nor an error message. Any idea what else I can try ?

Code:

Const ForReading = 1
Const TriStateUseDefault = -2

arrKeyWords = Array("Failed")
arrServers = array("Server1", "server2", "Server3")

strEmailFrom = "BackupAlert@mydomain.com"
strEmailTo = "me@mydomain.com"
strEmailSubject = "lines extracted from " & strFile
strSMTP = "smtp.mydomain.com"


Set objFSO = CreateObject("Scripting.FileSystemObject")


strDay = weekday(date)
select case (strDay)
case 1 : strDay = "sun"
case 2 : strDay = "mon"
case 3 : strDay = "tue"
case 4 : strDay = "wed"
case 5 : strDay = "thu"
case 6 : strDay = "fri"
case 7 : strDay = "sat"
end select

for i = 0 to ubound(arrServers)
strFile = "\\" & arrServers(i) & "\backups\" & strDay & "\log.txt"
set objStream = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
do while not objStream.AtEndOfStream
strLine = objStream.ReadLine
For Each strKeyWord in arrKeyWords
If InStr(1, strLine, strKeyWord, vbTextCompare) > 0 Then
strEmailBody = strEmailBody & strLine & vbCrLf & vbCrLf
Exit For
End If
Next
loop
next


If strEmailBody <> "" Then
Set objEmail = CreateObject("CDO.Message")
objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = strEmailSubject
objEmail.Textbody = strEmailBody
objEmail.Configuration.Fields.Item(" = 2
objEmail.Configuration.Fields.Item(" = strSMTP
objEmail.Configuration.Fields.Item(" = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End If
 
are you using Windows XP or Windows 7? [blue]In XP an array need to be traversed using for..loop. In Windows 7 a for..loop or for each..loop can be used.[/blue] [red]Also, throw a few msgboxes in there to see what code gets executed.[/red]

Code:
[red]msgbox "Traversing arrServers (size: " & ubound(arrServers) & ")"[/red] 
for i = 0 to ubound(arrServers)    
    strFile = "\\" & arrServers(i) & "\backups\" & strDay 
& "\log.txt"
    [red]msgbox "File: " & strFile[/red]    
    set objStream = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
    [red]msgbox "looping through " & strFile[/red]    
    do while not objStream.AtEndOfStream
        strLine = objStream.ReadLine
        [blue]For j = 0 to ubound(arrKeyWords)
            strKeyWord = arrKeyWords(j)[/blue]
            If InStr(1, strLine, strKeyWord, vbTextCompare) > 0 Then
                strEmailBody = strEmailBody & strLine & vbCrLf & vbCrLf
                Exit For
            End If
        Next
    loop
    [red]msgbox "Email: " & vbNewLine & strEmailBody[/red]    
next

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thank you Geates, the script now works like a charm. However, is there any way to identify which server the text is from. I tested with text files on 3 servers, 2 text files listed "process failed" and the other "successfully completed". Once the script was ran I did recieve an email with the 2 lines of the text for the servers that failed, but nothing to identify which server it was from. Thanks again for all your help, much appriceated.
 
Just add strFile to strEmailBody

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top