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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare 2 strings in textfile

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
hi there,

My script below searches through an error log for a string which is set in the script as a regular expression. When it finds the string(s) they are copied to another file.

searchstring = "Somestring"
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "^25-09-2007 Somestring"

Dim arrFileLines()
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", 1)
Set objFile1 = objFSO.OpenTextFile("C:\Scripts\results.txt", 2)

'check through text file and create 1d array
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close


'Read through array from last line to 1st line
For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
hello = arrFileLines(l)

'Check for regular expression and write to file if exists
Set colMatches = objRegEx.Execute(hello)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
Wscript.Echo (hello)
Next
objFile1.writeline (hello)
End If

next
objFile1.Close

What I need to do is compare the strings copied to see whch has the latest date stamp. Just to fill you in, the file which is searched is a log file and has entries in such as this

11-06-2010 Error 612 occured
12-06-2010 Error 612 occured

I need to differentiate between strings probably using the instr function to see which occurence is the later date. If this is too complicated is there something in the array where I can do this without creating a new output file with my results?

Thanks

James
 
Either you establish a from reading the output file or you directly implement the logic into the existing script.
[tt]
'your givens
[green]dim a
a=array("11-06-2010 Error 612 occured","12-06-2010 Error 612 occured")[/green]

dim dmax,i,d,atmp,dd
dmax=dateserial(100,1,1)
for i=0 to ubound(a)
d=split(a(i)," ")(0)
atmp=split(d,"-")
dd=dateserial(atmp(2),atmp(1),atmp(0)) 'assume continental date component order
if dd>dmax then
dmax=dd
end if
next
wscript.echo dmax 'in system default date format, otherwise, transform it to your desired format
[/tt]
 
Hi, Brilliant, thank you.

My array will have many enties in depending on how many times the error happens. Is there a way to just compare say the last 2 entries in the array and check for the oldest\youngest line using the dateserial function you mentioned?

Thanks

James
 
[tt]dim dmax,dmin,i,d,atmp,dd
dmax=dateserial(100,1,1)
dmin=dateserial(9999,1,1)
for i=0 to ubound(a)
d=split(a(i)," ")(0)
atmp=split(d,"-")
dd=dateserial(atmp(2),atmp(1),atmp(0)) 'assume continental date component order
if dd>dmax then
dmax=dd
end if
if dd<dmin then
dmin=dd
end if
next
wscript.echo dmax & vbcrlf & dmin
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top