vbscript - VB script to scan latest log file for errors -
i have vb script scans mentioned log file errors , sends notification through email.
how can scan latest log file in folder? example, filename1.070615 (filename1.mmddyy) log file. after size, logfile switches new file same name different date: filename1.070615.
cdosendusingport = 2, _ const forreading = 1 dim intstartatline, strfilecreateddate, i, strresults, strtexttoscanfor, bstartfromscratch dim strlastcheckedfor, strarraytostring, strsubject, strmailfrom, strmailto strmailto = "<emailaddress>" strmailfrom = "<fromaddress>" strsubject = "log scanner" strsmtpserver = "x.x.x.x" filetoread = "d:\log\filename1.mmddyy" arrtexttoscanfor = array("error","another thing") set wshshell = wscript.createobject("wscript.shell") searchkey = replace(replace(filetoread,":",""),"\","_") on error resume next strlastfilecheckedcreatedate = wshshell.regread("hkcu\software\rdscripts\checktxtfile\" & searchkey & "\createdate") strlastfilelastlinechecked = wshshell.regread("hkcu\software\rdscripts\checktxtfile\" & searchkey & "\lastlinechecked") strlastcheckedfor = wshshell.regread("hkcu\software\rdscripts\checktxtfile\" & searchkey & "\checkforstring") ilastcheckedline = wshshell.regread("hkcu\software\rdscripts\checktxtfile\" & searchkey & "\lastlinechecked") on error goto 0 set objfso = wscript.createobject("scripting.filesystemobject") set varfile = objfso.getfile(filetoread) arrlastcheckedfortext = split(strlastcheckedfor,",") strfilecreatedate = varfile.datecreated strfilemodifieddate = varfile.datelastmodified sstatus = "<li>using mail server: " & strsmtpserver & "</li><li>running from: " & wscript.scriptfullname & "</li>" set objtextfile = objfso.opentextfile(filetoread, forreading) objtextfile.readall ilinecount = objtextfile.line objtextfile.close if strlastcheckedfor = "" bstartfromscratch = true sstatus = sstatus & "<li>first run of script against string search</li>" & vbcrlf elseif ubound(arrtexttoscanfor) <> ubound(arrlastcheckedfortext) bstartfromscratch = true sstatus = sstatus & "<li>count of string search criteria has changed</li>" & vbcrlf else each stritem in arrtexttoscanfor else bstartfromscratch = true 'msgbox strresults end if if bstartfromscratch = true sstatus = sstatus & "<li>string search criteria not match prior search</li>" & vbcrlf end if next end if if cint(ilinecount) < cint(ilastcheckedline) bstartfromscratch = true sstatus = sstatus & "<li>last line checked (" & ilastcheckedline & ") greater total line count (" & ilinecount & ") in file</li>" end if if cstr(strfilecreatedate) = cstr(strlastfilecheckedcreatedate) , bstartfromscratch <> true intstartatline = strlastfilelastlinechecked if bstartfromscratch <> true sstatus = sstatus & "<li>continuing search line " & intstartatline & "</li>" & vbcrlf end if elseif strfilecreatedate <> strlastfilecheckedcreatedate or bstartfromscratch = true intstartatline = 0 if bstartfromscratch <> true sstatus = sstatus & "<li>file created date has changed, starting search line 0</li>" & vbcrlf end if end if = 0 dim strnextline each stritem in arrtexttoscanfor strarraytostring = strarraytostring & delim & stritem delim = "," next set objtextfile = objfso.opentextfile(filetoread, forreading) while objtextfile.atendofstream <> true if < cint(intstartatline) objtextfile.skipline else 'msgbox strnextline = objtextfile.readline each stritem in arrtexttoscanfor if instr(lcase(strnextline),lcase(stritem)) strresults = "<span style='font-family:courier-new;color:#696969'><span style='font-weight:bold;background-color:#bef3f3'>line " & & ":</span> " & replace(lcase(strnextline),lcase(stritem),"<span style='background-color:#ffff81'>" & stritem & "</span>") & "</span><br>" & vbcrlf & strresults bsendmail = true end if next end if = + 1 loop objtextfile.close set wshshell = createobject("wscript.shell") 'let's save our settings next time. wshshell.regwrite "hkcu\software\rdscripts\checktxtfile\" & searchkey & "\filechecked", filetoread, "reg_sz" wshshell.regwrite "hkcu\software\rdscripts\checktxtfile\" & searchkey & "\createdate", strfilecreatedate, "reg_sz" wshshell.regwrite "hkcu\software\rdscripts\checktxtfile\" & searchkey & "\lastlinechecked", i, "reg_sz" wshshell.regwrite "hkcu\software\rdscripts\checktxtfile\" & searchkey & "\lastscanned", now, "reg_sz" wshshell.regwrite "hkcu\software\rdscripts\checktxtfile\" & searchkey & "\checkforstring",strarraytostring, "reg_sz" set wshshell = nothing strfilesummary = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>file path:</td><td>" & filetoread & "</td></tr>" strfilecreatedatesummary = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>created date:</td><td>" & strfilecreatedate & "</td></tr>" strfilemodifieddatesummary = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>modified date:</td><td>" & strfilemodifieddate & "</td></tr>" strarraysummary = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>text string(s):</td><td>" & strarraytostring & "</td></tr>" strfilelinesummary = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>last line checked:</td><td>" & & "</td></tr>" strsummary = strfilesummary & strfilecreatedatesummary & strfilemodifieddatesummary & strarraysummary & strfilelinesummary strbodycontent = "<table style='font-family:calibri;'>" & strsummary & "</table><br><br><span style='font-size:large;'>entries:</span><br>" & strresults & "<div style='padding-top:30px;font-size:x-small'><br><div style='font-weight:bold;font-family:calibri;color:black;'>job details:<ul style='font-weight:normal;font-family:calibri;color:darkgray;'>" & sstatus & "</ul></div></div>" on error goto 0 'send email if need be. if bsendmail = true call sendmail(strmailfrom,strmailto,strsubject,strbodycontent) '------------------------------------------------------------------------ 'function emailfile - email warning file '------------------------------------------------------------------------ function sendmail(strfrom,strto,strsubject,strmessage) dim imsg, iconf, flds on error goto 0 '// create cdo connections. set imsg = createobject("cdo.message") set iconf = createobject("cdo.configuration") set flds = iconf.fields '// smtp server configuration. flds .item(cdosendusingmethod) = cdosendusingport '// set smtp server address here. .item(cdosmtpserver) = strsmtpserver .update end '// set message properties. imsg set .configuration = iconf .to = strmailto .from = strmailfrom .subject = strsubject .htmlbody = strmessage end 'imsg.htmlbody = strmessage '// send message. imsg.send ' send message. if cstr(err.number) <> 0 else end if end function
it bit easier if log files named filename1.yymmdd.
nevertheless, can use regex not verify filename swap date components put them in our desired format!
set re = new regexp re.pattern = "^(filename1\.)(\d{2})(\d{2})(\d{2})$" set fso = createobject("scripting.filesystemobject") each objfile in fso.getfolder("d:\log").files if re.test(objfile.name) strcomparename = re.replace(objfile.name, "$1$4$2$3") if strcomparename > strlatest strlatest = strcomparename end if next ' switch name back... strlatest = re.replace(strlatest, "$1$3$4$2") wscript.echo "the latest file is: " & strlatest this line:
strcomparename = re.replace(objfile.name, "$1$4$2$3") changes format mmddyy yymmdd , saves in string comparison.
once we've finished our comparisons, need take latest file found , reverse process:
strlatest = re.replace(strlatest, "$1$3$4$2") to original filename back!
Comments
Post a Comment