Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
091871271700If the same number is in each text file I need the script to identify it with a wscript.echo.
Thanks!

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"

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

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!

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)

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!

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.quitmight be buggy, like i said i haven't tested it.

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"

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |