Solved

Files comparing script not working properly

Posted on 2012-03-29
11
363 Views
Last Modified: 2012-04-03
Hello Scriting gurus ..  I've created a vb script (.hta) to compare two files  the script intended was like below

1) One text to box (A)to give a list of files (.txt)in each line like  (A1,A2,A3)
2) Another text box (B) to give a list of files (.txt)in each line like  (B1,B2,B3)
when i run the script it should compare A1 file with B1 file and list the matching line as ok and non matching line as not ok.

so what is the problem now ? so far script is satisfying few but not all.  the problems are listed below.

1) Say I've given few files A1,A2,A3 and first text box and B1,B2,B3 in second text box it will skip first two files and compare only the last ie A3 and B3 only.
2)  And if A3 is comparing with a file in B3 and if the B3 files is having blank lines it says Ok that should not be

Files are giving by their unc paths like   E:\A1.txt etc any thoughts and if you need more info i can provide that as well.


Open in new window


<head>
<title>Read the Error Lines of a Log File</title>
<HTA:APPLICATION
     APPLICATIONNAME="Read the Error Lines of a Log"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
>
</head>

<script language="VBScript">

    Sub GetProcesses

On Error Resume Next

strHTML = "<table border='1' style='border-collapse: collapse' "  & _
        "bordercolor='#111111' width='100%' id='Table1' >"


strText = Servers.Value
    arrLines = Split(strText, vbCrLf)
    For Each strLinee in arrLines
strComputer = strLinee
   
strText2 = Servers2.Value
    arrLines2 = Split(strText2, vbCrLf)
    For Each strLinee2 in arrLines2

strcomputer2 = strLinee2


If Len (strComputer) <= 0 Then

strHTML = strHTML & "**Please check the path given**" & "<br>"
Else

strHTML = strHTML & ":::::::::::::::::::::::::::::::::::::::::::::::::::Searching for error on " & strComputer & ":::::::::::::::::::::::::::::::::::::::::::::::::::" & "<br>"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile( strComputer , 1)
Set objFile2 = objFSO.OpenTextFile( strComputer2 , 1)
'strLine = objFile.ReadLine

If Err.Number <> 0 Then
strHTML = strHTML & Err.Description & "<br>"
Else
' strLine2 = objFile2.ReadAll


strHTML = "<table border='1' style='border-collapse: collapse' "  & _
        "bordercolor='#111111' width='100%' id='Table1' >"
       
strHTML = strHTML & "<td width='50%'>" & strComputer & "</td>"  
strHTML = strHTML & "<td width='8%'>" & "File Name" & "</td>"
strHTML = strHTML & "<td width='50%'>" & strComputer2 & "</td>"        
Do Until objFile.AtEndOfStream And objfile2.AtEndOfStream
 
     strLine = objFile.ReadLine
 
     strLine2 = objfile2.ReadLine


    IF InStr(strLine, strLine2) <> 0  then
'    strHTML = strHTML & InStr(strLine, strLine2) & "<br>"
              
strHTML = strHTML & "<tr>"
strHTML = strHTML & "<td width='50%'>" & strLine & "</td>"
strHTML = strHTML & "<td width='8%'>" & " OK " & "<br>"
strHTML = strHTML & "<td width='50%'>" & strLine2  & "</td>"

Else


strHTML = strHTML & "<tr>"
        strHTML = strHTML & "<td width='50%'>" & strLine & "</td>"
        strHTML = strHTML & "<td width='8%'>" & "Not OK" & "</td>"
        strHTML = strHTML & "<td width='50%'>" & strLine2 & _
            "</td>"            
'        strHTML = strHTML & strLine &"::::::IS not Matching with :::::::::::: "& strLine2 & "<br>"
       
       
'If intResult <> 0 Then
'strHTML = strHTML & strLine & "<br>"



End If
Loop
End If
End If         
Next
Next  
'objFile.Close



strHTML = strHTML & "</table>"
DataArea.InnerHTML = strHTML
End Sub







</script>

<body>


Please enter the path of the log files you want to check
<textarea name="Servers" id="Servers" rows="10" cols="110"></textarea>
<textarea name="Servers2" id="Servers2" rows="10" cols="110"></textarea>
<input type="button" value="Compare Files" name="run_button"  onClick="GetProcesses">
<p>
<span id = "DataArea"></span>

</body>
0
Comment
Question by:sujithmd
  • 6
  • 4
11 Comments
 
LVL 26

Expert Comment

by:Leon Fester
ID: 37784393
Do you really need to use the script to check comparisons?
Have a look at the following free apps that do the same thing.
File and folder compare software

http://www.grigsoft.com/download-windiff.htm
http://kdiff3.sourceforge.net/
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37785006
Hi, it looks like the first problem is due to the script reading the file names separated by a line feed, not a comma.  So you should enter
\\server\share\filea.txt
\\server\share\fileb.txt

and not
\\server\share\filea.txt,\\server\share\fileb.txt

For the second issue, replace this:
    IF InStr(strLine, strLine2) <> 0  then

with this
    If StrComp(strLine, strLine2, vbTextCompare) = 0 Then

Regards,

Rob.
0
 
LVL 5

Author Comment

by:sujithmd
ID: 37785804
Thanks Rob for the replay  the second problem is completely gone now  however the first one remains as you mentioned I was giving the files line by line only as you said in your example if give the files in each text box it compares only last two text box ie it will compare fileb1.txt with fileb2.txt also i doubt its just a false reporting or so ? since no errors are shown or is it the result of first file comparison is over written by the last one ? any thoughts  ?

\\server\share\filea1.txt
\\server\share\fileb1.txt

\\server\share\filea2.txt
\\server\share\fileb2.txt
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37792323
Hi, the solution to your second problem was a few logic errors in the way it wrote the output.  It was comparing the files, it just wasn't writing the data to the page.

Regards,

Rob.

<head>
<title>Read the Error Lines of a Log File</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Read the Error Lines of a Log"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
>
</head>

<script language="VBScript">

Sub GetProcesses
	On Error Resume Next
	Err.Clear
	strHTML = "<table border='1' style='border-collapse: collapse' "  & _
        "bordercolor='#111111' width='100%' id='Table1' >"

	strText = Servers.Value
	arrLines = Split(strText, vbCrLf)
	strText2 = Servers2.Value
	arrLines2 = Split(strText2, vbCrLf)
	For Each strLinee in arrLines
		strComputer = strLinee
    
		For Each strLinee2 in arrLines2
			strcomputer2 = strLinee2
			If Len (strComputer) <= 0 Then
				strHTML = strHTML & "**Please check the path given**" & "<br>"
			Else
				strHTML = strHTML & ":::::::::::::::::::::::::::::::::::::::::::::::::::Searching for error on " & strComputer & ":::::::::::::::::::::::::::::::::::::::::::::::::::" & "<br>"

				Set objFSO = CreateObject("Scripting.FileSystemObject")
				Set objFile = objFSO.OpenTextFile( strComputer , 1)
				Set objFile2 = objFSO.OpenTextFile( strComputer2 , 1)
				'strLine = objFile.ReadLine

				If Err.Number <> 0 Then
					strHTML = strHTML & Err.Description & "<br>"
				Else 
					' strLine2 = objFile2.ReadAll

					strHTML = strHTML & "<table border='1' style='border-collapse: collapse' "  & _
						"bordercolor='#111111' width='100%' id='Table1' >"
        
					strHTML = strHTML & "<td width='50%'>" & strComputer & "</td>"  
					strHTML = strHTML & "<td width='8%'>" & "File Name" & "</td>" 
					strHTML = strHTML & "<td width='50%'>" & strComputer2 & "</td>"        
					Do Until objFile.AtEndOfStream And objfile2.AtEndOfStream
 
						strLine = objFile.ReadLine

						strLine2 = objfile2.ReadLine

						If StrComp(strLine, strLine2, vbTextCompare) = 0 Then
							'strHTML = strHTML & InStr(strLine, strLine2) & "<br>"
							strHTML = strHTML & "<tr>"
							strHTML = strHTML & "<td width='50%'>" & strLine & "</td>"
							strHTML = strHTML & "<td width='8%'>" & " OK " & "<br>"
							strHTML = strHTML & "<td width='50%'>" & strLine2  & "</td>"
						Else 
							strHTML = strHTML & "<tr>"
							strHTML = strHTML & "<td width='50%'>" & strLine & "</td>"
							strHTML = strHTML & "<td width='8%'>" & "Not OK" & "</td>" 
							strHTML = strHTML & "<td width='50%'>" & strLine2 & "</td>"            
							'strHTML = strHTML & strLine &"::::::IS not Matching with :::::::::::: "& strLine2 & "<br>"

							'If intResult <> 0 Then strHTML = strHTML & strLine & "<br>"
						End If 
					Loop
				End If 
				objFile.Close
				objFile2.Close
			End If         
			strHTML = strHTML & "</table>"
			DataArea.InnerHTML = DataArea.InnerHTML & strHTML
		Next
	Next  
	'objFile.Close
End Sub

</script>

<body>


Please enter the path of the log files you want to check 
<textarea name="Servers" id="Servers" rows="10" cols="110"></textarea>
<textarea name="Servers2" id="Servers2" rows="10" cols="110"></textarea>
<input type="button" value="Compare Files" name="run_button"  onClick="GetProcesses">
<p>
<span id = "DataArea"></span>

</body>

Open in new window

0
 
LVL 5

Author Comment

by:sujithmd
ID: 37794931
Thanks Rob for the replay and rework of the code however now its showing more output than the file put for comparison say

Text box A
\\server\share\filea1.txt
\\server\share\fileb1.txt

Text box B
\\server\share\filea2.txt
\\server\share\fileb2.txt

the script is intended to compare the FILE in first line of Text box A with the FILE in first line of Text box B and write the out put and once its done it has to compare the FILE in second line of the Text box A with the File in second line of the Text box B. But now the script is showing more than 4 comparisons instead of two comparisons if I give files as in the above example any ideas ?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 65

Expert Comment

by:RobSampson
ID: 37798707
Yes, that's happening because it's written to compare the following:
FileA1.txt with FileA2.txt
FileA1.txt with FileB2.txt
FileB1.txt with FileA2.txt
FileB1.txt with FileB2.txt

so it compares each in the first text box with each in the second.

Let me rework it so that it does
FileA1.txt with FileA2.txt
FileA2.txt with FileB2.txt

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37799311
Hi, this should work.

Regards,

Rob.

<head>
<title>Read the Error Lines of a Log File</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Read the Error Lines of a Log"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
>
</head>

<script language="VBScript">

Sub GetProcesses
	DataArea.InnerHTML = ""
	On Error Resume Next
	Err.Clear
	strHTML = "<table border='1' style='border-collapse: collapse' "  & _
        "bordercolor='#111111' width='100%' id='Table1' >"

	strText = Servers.Value
	arrLines = Split(strText, vbCrLf)
	strText2 = Servers2.Value
	arrLines2 = Split(strText2, vbCrLf)
	If UBound(arrLines) < UBound(arrLines2) Then
		intFilesToCompare = UBound(arrLines)
		MsgBox "Text box 1 has less files than Text box 2.  Only " & intFilesToCompare + 1 & " file(s) will be compared."
	ElseIf UBound(arrLines) > UBound(arrLines2) Then
		intFilesToCompare = UBound(arrLines2)
		MsgBox "Text box 2 has less files than Text box 1.  Only " & intFilesToCompare + 1 & " file(s) will be compared."
	Else
		intFilesToCompare = UBound(arrLines)
		'MsgBox "Text boxes are the same.  " & intFilesToCompare + 1 & " file(s) will be compared."
	End If
	For intLine = 0 To intFilesToCompare
		strComputer = arrLines(intLine)
    	strcomputer2 = arrLines2(intLine)
		If Len (strComputer) <= 0 Then
			strHTML = strHTML & "**Please check the path given**" & "<br>"
		Else
			strHTML = strHTML & ":::::::::::::::::::::::::::::::::::::::::::::::::::Searching for error on " & strComputer & ":::::::::::::::::::::::::::::::::::::::::::::::::::" & "<br>"

			Set objFSO = CreateObject("Scripting.FileSystemObject")
			Set objFile = objFSO.OpenTextFile( strComputer , 1)
			Set objFile2 = objFSO.OpenTextFile( strComputer2 , 1)
			'strLine = objFile.ReadLine

			If Err.Number <> 0 Then
				strHTML = strHTML & Err.Description & "<br>"
			Else 
				' strLine2 = objFile2.ReadAll

				strHTML = strHTML & "<table border='1' style='border-collapse: collapse' "  & _
					"bordercolor='#111111' width='100%' id='Table1' >"
       
				strHTML = strHTML & "<td width='50%'>" & strComputer & "</td>"  
				strHTML = strHTML & "<td width='8%'>" & "File Name" & "</td>" 
				strHTML = strHTML & "<td width='50%'>" & strComputer2 & "</td>"        
				Do Until objFile.AtEndOfStream And objfile2.AtEndOfStream

					strLine = objFile.ReadLine

					strLine2 = objfile2.ReadLine

					If StrComp(strLine, strLine2, vbTextCompare) = 0 Then
						'strHTML = strHTML & InStr(strLine, strLine2) & "<br>"
						strHTML = strHTML & "<tr>"
						strHTML = strHTML & "<td width='50%'>" & strLine & "</td>"
						strHTML = strHTML & "<td width='8%'>" & " OK " & "<br>"
						strHTML = strHTML & "<td width='50%'>" & strLine2  & "</td>"
					Else 
						strHTML = strHTML & "<tr>"
						strHTML = strHTML & "<td width='50%'>" & strLine & "</td>"
						strHTML = strHTML & "<td width='8%'>" & "Not OK" & "</td>" 
						strHTML = strHTML & "<td width='50%'>" & strLine2 & "</td>"            
						'strHTML = strHTML & strLine &"::::::IS not Matching with :::::::::::: "& strLine2 & "<br>"

						'If intResult <> 0 Then strHTML = strHTML & strLine & "<br>"
					End If 
				Loop
			End If 
			objFile.Close
			objFile2.Close
		End If         
		strHTML = strHTML & "</table>"
		DataArea.InnerHTML = DataArea.InnerHTML & strHTML
	Next  
	'objFile.Close
End Sub

</script>

<body>


Please enter the path of the log files you want to check 
<textarea name="Servers" id="Servers" rows="10" cols="110"></textarea>
<textarea name="Servers2" id="Servers2" rows="10" cols="110"></textarea>
<input type="button" value="Compare Files" name="run_button"  onClick="GetProcesses">
<p>
<span id = "DataArea"></span>

</body>

Open in new window

0
 
LVL 5

Author Comment

by:sujithmd
ID: 37799597
Thanks Rob I had a detailed run on your latest code I Put four files in each text box.
Text Box A

FileA1.txt
FileA2.txt
FileA3.txt
FileA4.txt

Text Box B

FileB1.txt
FileB2.txt
FileB3.txt
FileB4.txt

what i noticed is the in the fourth run the script gives exactly the required output. Is there any we can suppress the other outputs ? as of now the script is running as below.

1) Compare FileA1 with File FileB1 - Write the output
2) Compare FileA2 with File FileB2 - Write the output here wired starts  (it writes the output of fileA1 and FileB1 comparison then writes the output of  fileA2 and FileB2 comparison)
3) Compare FileA3 with File FileB3 - It writes the output of fileA1 and FileB1 comparison then writes the output of  fileA2 and FileB2 comparison then writes the output of FileA3 and FileB3 comparison.
4) Compare FileA4 with File FileB4 - It writes the output of fileA1 and FileB1 comparison then writes the output of  fileA2 and FileB2 comparison then writes the output of FileA3 and FileB3 comparison and finally FileA4 and FileB4 comparison
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37800559
You're right.  Sorry I didn't notice that.  Try this.

Rob.

<head>
<title>Read the Error Lines of a Log File</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Read the Error Lines of a Log"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
>
</head>

<script language="VBScript">

Sub GetProcesses
	DataArea.InnerHTML = ""
	On Error Resume Next
	Err.Clear
	strHTML = "<table border='1' style='border-collapse: collapse' "  & _
        "bordercolor='#111111' width='100%' id='Table1' >"

	strText = Servers.Value
	arrLines = Split(strText, vbCrLf)
	strText2 = Servers2.Value
	arrLines2 = Split(strText2, vbCrLf)
	If UBound(arrLines) < UBound(arrLines2) Then
		intFilesToCompare = UBound(arrLines)
		MsgBox "Text box 1 has less files than Text box 2.  Only " & intFilesToCompare + 1 & " file(s) will be compared."
	ElseIf UBound(arrLines) > UBound(arrLines2) Then
		intFilesToCompare = UBound(arrLines2)
		MsgBox "Text box 2 has less files than Text box 1.  Only " & intFilesToCompare + 1 & " file(s) will be compared."
	Else
		intFilesToCompare = UBound(arrLines)
		'MsgBox "Text boxes are the same.  " & intFilesToCompare + 1 & " file(s) will be compared."
	End If
	For intLine = 0 To intFilesToCompare
		strComputer = arrLines(intLine)
    	strcomputer2 = arrLines2(intLine)
    	MsgBox "Comparing " & strComputer & " with " & strComputer2
		If Len (strComputer) <= 0 Then
			strHTML = "**Please check the path given**" & "<br>"
		Else
			strHTML = ":::::::::::::::::::::::::::::::::::::::::::::::::::Searching for error on " & strComputer & ":::::::::::::::::::::::::::::::::::::::::::::::::::" & "<br>"

			Set objFSO = CreateObject("Scripting.FileSystemObject")
			Set objFile = objFSO.OpenTextFile( strComputer , 1)
			Set objFile2 = objFSO.OpenTextFile( strComputer2 , 1)
			'strLine = objFile.ReadLine

			If Err.Number <> 0 Then
				strHTML = strHTML & Err.Description & "<br>"
			Else 
				' strLine2 = objFile2.ReadAll

				strHTML = strHTML & "<table border='1' style='border-collapse: collapse' "  & _
					"bordercolor='#111111' width='100%' id='Table1' >"
       
				strHTML = strHTML & "<td width='50%'>" & strComputer & "</td>"  
				strHTML = strHTML & "<td width='8%'>" & "File Name" & "</td>" 
				strHTML = strHTML & "<td width='50%'>" & strComputer2 & "</td>"        
				Do Until objFile.AtEndOfStream And objfile2.AtEndOfStream

					strLine = objFile.ReadLine

					strLine2 = objfile2.ReadLine

					If StrComp(strLine, strLine2, vbTextCompare) = 0 Then
						'strHTML = strHTML & InStr(strLine, strLine2) & "<br>"
						strHTML = strHTML & "<tr>"
						strHTML = strHTML & "<td width='50%'>" & strLine & "</td>"
						strHTML = strHTML & "<td width='8%'>" & " OK " & "<br>"
						strHTML = strHTML & "<td width='50%'>" & strLine2  & "</td>"
					Else 
						strHTML = strHTML & "<tr>"
						strHTML = strHTML & "<td width='50%'>" & strLine & "</td>"
						strHTML = strHTML & "<td width='8%'>" & "Not OK" & "</td>" 
						strHTML = strHTML & "<td width='50%'>" & strLine2 & "</td>"            
						'strHTML = strHTML & strLine &"::::::IS not Matching with :::::::::::: "& strLine2 & "<br>"

						'If intResult <> 0 Then strHTML = strHTML & strLine & "<br>"
					End If 
				Loop
			End If 
			objFile.Close
			objFile2.Close
		End If         
		strHTML = strHTML & "</table>"
		DataArea.InnerHTML = DataArea.InnerHTML & strHTML
	Next  
	'objFile.Close
End Sub

</script>

<body>


Please enter the path of the log files you want to check 
<textarea name="Servers" id="Servers" rows="10" cols="110"></textarea>
<textarea name="Servers2" id="Servers2" rows="10" cols="110"></textarea>
<input type="button" value="Compare Files" name="run_button"  onClick="GetProcesses">
<p>
<span id = "DataArea"></span>

</body>

Open in new window

0
 
LVL 5

Author Comment

by:sujithmd
ID: 37804635
Thanks a lot Rob its working as expected and at least 50% of my coding has gone now :-)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37804646
No problem.  Most of your code is still there, it's just reshuffled a bit, and a loop was removed.  Thanks for the grade.

Rob.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now