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

Browse for text files and Compare 2

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hi,

I found this code from "The Scripting Guy" for comparing 2 files and output changes. This works great for what I need.... What I would like to have is a "Browse For File" function for the text files. Is there any easy way to do this? Thanks for any help... I am going to keep searching in the meantime.

Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\Temp\NewRev.txt", ForReading)

strCurrentDevices = objFile1.ReadAll
objFile1.Close

Set objFile2 = objFSO.OpenTextFile("C:\Temp\OldRev.txt", ForReading)

Do Until objFile2.AtEndOfStream
    strAddress = objFile2.ReadLine
    If InStr(strCurrentDevices, strAddress) = 0 Then
        strNotCurrent = strNotCurrent & strAddress & vbCrLf
    End If
Loop

objFile2.Close

Wscript.Echo "Changes since last revision: " & vbCrLf & strNotCurrent

Set objFile3 = objFSO.CreateTextFile("C:\Temp\Changes.txt")

objFile3.WriteLine strNotCurrent
objFile3.Close
 
This FAQ claims to work for XP/2000, may need some changes if you are running Vista or 7.
faq329-5310
 
This link is a previous post that covers Win7
thread329-1604032

-Geates
 
Thanks for the info ... I did try the one that requires the use of "CommonDialog" but could NOT get it registered no matter what I did on Windows 7. I finally gave up on that. I did find the one here on Tek-Tips and it does work but I could not figure out how to merge my existing code in to that one.
 
Interesting. The author of thread329-1604032 gave up on the alternative solution. Can you link to the solution that you are having troubles integrating into you code?

-Geates
 

I am still trying to play around with the open file dialog... In the meantime, what I have so far places the changed line at the top of the output file. It places the complete file but I cannot figure out how to get just the changed line before and after?

Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\Temp\Program r2.txt", ForReading)

strCurrentDevices = objFile1.ReadAll
objFile1.Close

Set objFile2 = objFSO.OpenTextFile("C:\Temp\Program r1.txt", ForReading)

Do Until objFile2.AtEndOfStream
    strAddress = objFile2.ReadLine
    If InStr(strCurrentDevices, strAddress) = 0 Then
        strNotCurrent = strNotCurrent & strAddress & vbCrLf
    End If
Loop

objFile2.Close

Set objFile3 = objFSO.CreateTextFile("C:\Temp\Program Changes.txt")

objFile3.WriteLine "Changes made from last Revision: " & vbCrLf & vbCrLf & strNotCurrent & vbCrLf & _
"Last Revision File: " & vbCrLf & vbCrLf & strCurrentDevices
objFile3.Close
 
In the meantime, what I have so far places the changed line at the top of the output file. It places the complete file but I cannot figure out how to get just the changed line before and after?

I'm not following :( It seems to do for me what is expected. Can you post a piece of your 2 input files and the resulting output file?

NOTE: What happens when I use two input files containing the same content in a different order? The script outputs no changes because it is only checking to see if the content of file2 is in file1. (is this the problem your having?)

-Geates
 
crackoo ... thanks! This is just what I need, it works GREAT!
 
Geates.. At "strCurrentDevices" it will print the entire OLD file instead of just the line numbers that were changed.

Code:
Changes made from last Revision: 

N0001 (File Posted on 10/15/2012 10:28:12)
N0003 (Target Machine: A-1022)
N0022 X2.942 Y7.000
N0030 X5.779 Y8.476

Last Revision File: 

N0000 (File Posted with ACE Teach v2.0.40.0)
N0001 (File Posted on 10/10/2012 12:47:54)
N0002 (Part Number: 41-054020-10)
N0003 (Target Machine: A-1024)
N0004 (Solder Alloy: Leaded)
N0005 (Tool 1: 9mm)
N0006 (Tool 2: 6mm)
 
Where is [tt]At "strCurrentDevices"[/tt]? Could it be because strCurrentDevices is set to the contents of the entire OLD file and then printed at the end of the script?

Code:
strCurrentDevices = objFile1.ReadAll
...
objFile3.WriteLine "Changes made from last Revision: " & vbCrLf & vbCrLf & strNotCurrent & vbCrLf & _
"Last Revision File: " & vbCrLf & vbCrLf & [red]strCurrentDevices[/red]

-Geates
 
Yes, that is where I am stuck. How to have it write just the lines that were changed like: strNotCurrent
 
I see. There really isn't anyway to do it with the structure of your script. You are looking for a single green M&M in a bowl of blue M&Ms. Against which blue M&M are your comparing to see if it's green? All of them! Like the green M&M in a bowl of blue M&Ms, you are checking to see if a single line from file2 exists in all of file1. The only thing you can get from this is if the line exists or not in file1.

To get around this, you need to compare each line of each file. Crackoo HTA solution works because it compares the files line by line.

Code:
 Do Until objSourceFile.AtEndOfStream Or objSourceFile2.AtEndOfStream
   vrNumLigne = vrNumLigne + 1
[red]
   vrLigne = objSourceFile.ReadLine
   vrLigne2 = objSourceFile2.ReadLine
   vrComprLign = StrComp(vrLigne, vrLigne2, 1)
[/red]
...
 Loop

NOTE: Crackoo's solution only checks line by line. So if the only difference between the two files was a sinlge line, the solution would return the rest of the file from that single line as being different. This goes to show that, typically, accuracy equals complexity.

-Geates
 

Thanks Geates... I appreciate your time and help as always.

Crackoo's file is working nicely so far for what I need. I do have a couple questions since I have not yet got into hta programs. How can I change the button sizes at the bottom? I found where I can change the length but not the height?
 
CSS

An HTA is essentially an instance of an IE browser without any controls. Like with a website, an HTA can use several different technologies, including html, css, javscript, vbscript. Crackoo's HTA is marked up with html, styled with css, and "functionalized?" with vbscript. The thing to which you really need to pay attention is that HTAs are Microsoft solutions and typically always contain proprietary metrics so they will likely not work on non MS systems.

In this HTA, the buttons are markedup using an [ignore]<input>[/ignore] tag with a [tt]type="button"[/tt] (line 146 - 151). They are then styled using CSS (specifically, lines 15 - 23). Modify these lines of code to style the button to your liking.

-Geates
 
Nu2Java said:
How can I change the button sizes at the bottom? I found where I can change the length but not the height?

HTML:
<input type="button" style="width: 200px" style="height: 40px" name="OK" id="OK" value="Comparer les Fichiers" onclick="File2Compare File1,File2">
<input type="button" style="width: 140px" style="height: 40px"name="Cancel" id="Cancel" value="Sortir" onclick="OnClickButtonCancel">
 
Thanks ... I was able to get this fine tuned a little bit. I am understanding a bit more of what you are saying about the changes and line by line method.

I am really trying to create a change history script for a machine. When this file gets read by the machine, if there are any changes or additional lines, it will auto renumber the entire file, like this starting with the N0000:
Code:
N0000 (File Posted with ACE Teach v2.0.40.0)
N0001 (File Posted on 10/10/2012 12:47:54)
N0002 (Part Number: 41-054020-10)
N0003 (Target Machine: A-1024)
N0004 (Solder Alloy: Leaded)
N0005 (Tool 1: 9mm)
N0006 (Tool 2: 6mm)

Add or Remove a line and it changes to this:
Code:
N0000 (File Posted with ACE Teach v2.0.40.0)
N0001 (ADD A NEW LINE)
N0002 (File Posted on 10/10/2012 12:47:54)
N0003 (Part Number: 41-054020-10)
N0004 (Target Machine: A-1024)
N0005 (Solder Alloy: Leaded)
N0006 (Tool 1: 9mm)
N0007 (Tool 2: 6mm)

Based on this, I think what I need is my original piece of code with the great functionality of the HTA browser and then a way to ignore the first 5 characters in the file. I am going to keep working on this, but if you can help put me in the right direction, I would greatly appreciate it.
 
You can easily remove the first 6 chars (5 for the number + 1 space) by setting the line equal to the rightmost part of the string. Keep in mind, however, that you are getting rid of an "index" by doing this. The number (e.g. N00001) uniquely identifies a row and may come in handy. Perhaps you'll want to hang on to this information.

Code:
Do Until objSourceFile.AtEndOfStream Or objSourceFile2.AtEndOfStream
   vrNumLigne = vrNumLigne + 1

   vrLigne = objSourceFile.ReadLine
   vrLigne2 = objSourceFile2.ReadLine
[red]
   vrLigne = right(vrLigne, len(vrLigne) - 6)
   vrLigne2 = right(vrLigne, len(vrLigne2) - 6)
   intLineNum = left(vrLigne, 6)
   intLineNum2 = left(vrLigne2, 6)
[/red]
   vrComprLign = StrComp(vrLigne, vrLigne2, 1)
...
loop

-Geates
 
Ok, this is what I ended up with. I took my original piece of code and worked it into crackoo's hta code. It works pretty good and writes out any changed or new lines. I haven't figured out how to add in your piece of code to ignore the 6 characters. I did use that on crackoo's original and got strange results. I will have a better answer on that shortly.


Code:
<html>
<head>
<Title>Compare Files / Output Changes</Title>
<HTA:APPLICATION

 
APPLICATIONNAME="File2Compare"
ID="File2Compare"
BORDER="dialog"
INNERBORDER="no"
MAXIMIZEBUTTON="no"
SCROLL="no"
VERSION="1.0"/>

<style type="text/css">

 
/* Theme created by subBlue design for phpBB, [URL unfurl="true"]http://www.subBlue.com[/URL] avec queleques modification apporté par Hackoo

 
  ©2001, 2005 phpBB Group, [URL unfurl="true"]http://www.phpbb.com/[/URL]    */

 
input,button

  {

 border:1px solid #FF6A22;
 background-color:lightblue;
 font-family:Courier;

 font-size:16px;
   color:#0000DD;
      font-weight:; 
                     }

 
textarea{background-color:lightgray;
Color:black;}

 
 body { 

   overflow:auto;
     position:relative;
   
   top:5px;
    left:5px;

scrollbar-face-color: #DEE3E7;
scrollbar-highlight-color: #FFFFFF;
scrollbar-shadow-color: #DEE3E7;
scrollbar-3dlight-color: #D1D7DC;
scrollbar-arrow-color:  #006699;
scrollbar-track-color: #EFEFEF;
scrollbar-darkshadow-color: #98AAB1; 
background-color:black;
margin-top:6px;

color:none;

filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='lightblue', EndColorStr='blue'); 

 }

 
button  {       
border:18px solid #0000DD;
 background-color:#0000DD;
  font-family:Verdana;
   font-size:18px;
     height:16px;
      font-weight:bold; 
        cursor:hand;    }

 
div.forumline {margin-left:10px; background-color: black; border: 4px #006699 solid; float:left; 
height:600px; width:650px; overflow:scroll;  }
div.form {float:left;font-family:Arial;} 
div.toolbar {width:145%; font-family:Arial;}

 
th,td,p { font-family: Verdana, Arial, Helvetica, sans-serif }

  hr      { height: 8px; border: solid #D1D7DC 8px; border-top-width: 1px;}

.gen { font-size : 16px; }

.genmed { font-size : 16px; }

.gensmall { font-size : 16px; }

.gen,.genmed,.gensmall { color : #000000; }

a.gen,a.genmed,a.gensmall { color: #006699; text-decoration: none; }

a.gen:hover,a.genmed:hover,a.gensmall:hover     { color: #DD6900; text-decoration: underline; }

input.button {  background-color : #0000DD;
                color : #000000; cursor:hand;
                font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; }

 
input.longbt {  background-color : #0000DD;
                color : #000000;
                font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; width:140px}

 
.code        { font-family: Courier, 'Courier New', sans-serif; font-size: 11px; color: #006600;
               background-color: #FAFAFA; border: #D1D7DC; border-style: solid;

         border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px }

.quote {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #444444; line-height: 145%;

         background-color: #FAFAFA; border: #D1D7DC; border-style: solid;
         border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px }

.err{color: white; background-color: black; font-family: Verdana, Arial, Helvetica, sans-serif }

#FolderPath{color:#FF0000}

 
</style>
</head>
<script language="VBScript">

Function File2Compare(File1,File2)
Const ForReading = 1, ForWriting = 2

Dim objFSO,objSourceFile,objSourceFile2
Title ="File to Compare" 

File1=File1.value
File2=File2.value

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFile = objFSO.OpenTextFile(File1, ForReading)

strCurrentDevices = objSourceFile.ReadAll
objSourceFile.Close

Set objSourceFile2 = objFSO.OpenTextFile(File2, ForReading)

Do Until objSourceFile2.AtEndOfStream
    strAddress = objSourceFile2.ReadLine
    If InStr(strCurrentDevices, strAddress) = 0 Then
        strNotCurrent = strNotCurrent & strAddress & vbCrLf
    End If
Loop

objSourceFile2.Close

    Dim fso, f 

    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set f = fso.OpenTextFile(File1 & "_HISTORY", ForWriting,true)

	
	f.writeLine String(90,"*")
    f.writeline "Compared this file: " & File1
	f.writeline "Against this file: " & File2
    f.writeLine String(90,"*")
	f.writeline vbNewLine & "Changes to Last Revision: " & vbNewLine & vbNewLine & strNotCurrent
    f.writeline	"New Revision File: " & vbNewLine & vbNewLine & strCurrentDevices
	
	
txtBody.value = "Changes made from last Revision: " & vbNewLine & vbNewLine & strNotCurrent & vbCrLf & _
"New Revision File: " & vbCrLf & vbCrLf & strCurrentDevices

f.close
End Function

Sub Window_OnLoad

CenterWindow 820, 680

End Sub

 
Sub CenterWindow(x,y)

window.resizeTo x, y

iLeft = window.screen.availWidth/2 - x/2
itop = window.screen.availHeight/2 - y/2

window.moveTo ileft, itop

End Sub

 
Sub OnClickButtonOK()

window.Close

End Sub

 
Sub OnClickButtonCancel()

window.Close

End Sub

 
</script>
<center>

<FONT face="Courier" color="black" size=2>Compare File #1:</FONT><input type="file" size="35" name="file1" id="file1" /><br>

<FONT face="Courier" color="black" size=2>Compare File #2:</FONT><input type="file" size="35" name="file2" id="file2" /><br>

<FONT face="Courier" color="black" size=2>Results of Comparison:</FONT><br/>

<textarea id="txtBody" rows="23" cols="65"></textarea><br><br>

<input type="button" style="width:165;height:65" name="OK" id="OK" value="COMPARE FILES" onclick="File2Compare File1,File2">

<input type="button" style="width:165;height:65" name="Cancel" id="Cancel" value="EXIT PROGRAM" onclick="OnClickButtonCancel">

</td></tr>
</table>
</table>
</body>
</html>
 
I am stuck now... I tried to add your code to my existing code (above) and it seems to delete MOST of the first line in the file. Not sure what needs to change. This is what I placed in my current...

Code:
Do Until objSourceFile2.AtEndOfStream
    strAddress = objSourceFile2.ReadLine
 
   strCurrentDevices = right(strCurrentDevices, len(strCurrentDevices) - 6)
   strAddress = right(strAddress, len(strAddress) - 6)
   intLineNum = left(strCurrentDevices, 6)
   intLineNum2 = left(strAddress, 6)
	
    If InStr(strCurrentDevices, strAddress) = 0 Then
        strNotCurrent = strNotCurrent & strAddress & vbCrLf
    End If
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top