VB Script Compare two Text files for Duplicat

November 3, 2009 at 11:19:48
Specs: Windows Vista
I’m just getting started with VB Script and I need to figure out how to compare two text files for duplicate numbers and then echo those duplicates.
For example each text files contains numbers like:

091871271500
091871271600
091871271700

If the same number is in each text file I need the script to identify it with a wscript.echo.

Thanks!


See More: VB Script Compare two Text files for Duplicat

Report •


#1
November 4, 2009 at 11:02:21
This should do the trick where file1.txt is the first tile and file2.txt is the second.

Const ForReading = 1, ForWriting = 2
Dim fso, txtFile, txtFile2, strLine1, strLine2, strMatch
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile1 = fso.OpenTextFile("test1.txt", ForReading)


Do Until txtFile1.AtEndOfStream
strMatch = False
strLine1 = txtFile1.Readline
Set txtFile2 = fso.OpenTextFile("test2.txt", ForReading)
Do Until txtFile2.AtEndOfStream
strLine2 = txtFile2.Readline
If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
strMatch = True
Else
End If
Loop
txtFile2.Close
If strMatch <> False then
Wscript.Echo strLine1
End If
Loop
Wscript.Echo " "
Wscript.Echo "Done"


Report •

#2
November 4, 2009 at 11:28:46
You are the man!

I tested it and it works great!

I have seriously been sitting here all day reading up on VB in hopes I could accomplish this.


Thanks so much!!!


Report •

#3
November 5, 2009 at 08:11:38
Would it be possible to modify this to search through only one text file and also echo the duplicates?

I've tried modifying the script to do this, but have no luck.

Thanks!


Report •

Related Solutions

#4
November 5, 2009 at 22:30:01
Do you want a count? or just a boolean "yes/no" if there is more than one string? (in ref. to the one-file scanner)

Report •

#5
November 6, 2009 at 05:57:49
I just needed a way to identify the duplicate names within the text file. Just a wscript.echo that would pop up identifying each duplicate line.


Thanks!


Report •

#6
November 7, 2009 at 19:46:43
this might work, i haven't tried it yet. two files are required however, even though it is just testing one file, if you want to test each line of the file against the rest of the file. be sure to test it on dummy files in dummy directory so nothing important gets destroyed.

Const ForReading = 1, ForWriting = 2
Dim fso, txtFile1, txtFile2, strLine1, strLine2, strMatch
Set fso = CreateObject("Scripting.FileSystemObject")


'first copy file to temp because it gets destroyed
fso.Copyfile"test1.txt","temp1"
fso.Copyfile"test1.txt","temp2"
N=1
do while N>0
N=0
Set txtFile2 = fso.OpenTextFile("temp2", ForWriting)
set txtFile1=fso.OpenTextFile("temp1",ForReading)
strMatch = False
Do Until txtFile1.AtEndOfStream
if N=0 then
strLine1 = txtFile1.Readline
N=1
else
strLine2 = txtFile1.Readline
If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
strMatch = True
N=N+1
else
txtfile2.Writeline strLine2
end if
end if
loop
txtFile1.Close
txtFile2.Close
If strMatch <> False then
Wscript.Echo strLine1&" occurs "&N&" times"
End If
if N>0 then
fso.Copyfile "temp2", "temp1"
end if
Loop
wscript.echo "done."
wscript.quit

might be buggy, like i said i haven't tested it.


Report •

#7
November 8, 2009 at 12:34:45
VBScript reads in file1, then scans file2 for the same line. If it encounters it, the duplicate line is written to output.

Const file1 = "text1.txt"
Const file2 = "text2.txt"
Const output = "out.txt"

Set dic = CreateObject("Scripting.Dictionary")
Set fso = CreateObject("Scripting.FileSystemObject")
Set out = fso.OpenTextFile(output, 2, True)
With fso.OpenTextFile(file1)
  Do Until .AtEndOfStream
    line = .ReadLine
    If Not dic.Exists(line) Then _
      dic.Add line, 0
  Loop
End With

With fso.OpenTextFile(file2)
  Do Until .AtEndOfStream
    line = .ReadLine
    If dic.Exists(line) Then _
      out.WriteLine line
  Loop
End With
WScript.Echo "Done"


Report •

#8
November 9, 2009 at 04:40:09
I will try the scripts out.


I really appreciate all of the help!

Thank you all!


Report •


Ask Question