Computing.Net > Forums > Programming > VB Script Compare two Text files for Duplicat

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VB Script Compare two Text files for Duplicat

Reply to Message Icon

Name: dubhubb
Date: November 3, 2009 at 11:19:48 Pacific
OS: Windows Vista
Subcategory: General
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: twelvecodespent
Date: November 4, 2009 at 11:02:21 Pacific
Reply:

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"


1

Response Number 2
Name: dubhubb
Date: November 4, 2009 at 11:28:46 Pacific
Reply:

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!!!


0

Response Number 3
Name: dubhubb
Date: November 5, 2009 at 08:11:38 Pacific
Reply:

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!


0

Response Number 4
Name: nbrane
Date: November 5, 2009 at 22:30:01 Pacific
Reply:

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)


0

Response Number 5
Name: dubhubb
Date: November 6, 2009 at 05:57:49 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: nbrane
Date: November 7, 2009 at 19:46:43 Pacific
Reply:

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.


0

Response Number 7
Name: Razor2.3
Date: November 8, 2009 at 12:34:45 Pacific
Reply:

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"


0

Response Number 8
Name: dubhubb
Date: November 9, 2009 at 04:40:09 Pacific
Reply:

I will try the scripts out.


I really appreciate all of the help!

Thank you all!


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: VB Script Compare two Text files for Duplicat

Compare two text files with spaces www.computing.net/answers/programming/compare-two-text-files-with-spaces/19886.html

Merging two text files, alternating order. www.computing.net/answers/programming/merging-two-text-files-alternating-order/19292.html

Compare two text files, output diff to 3rd www.computing.net/answers/programming/compare-two-text-files-output-diff-to-3rd/19516.html