1

I'm new to VBScript and I'm trying to:

  • Read a text file and find duplicates
  • Copy the duplicate line to another text file

I've already have the following code. I've Google-ed my question but most answers are comparison between two files. Any idea how to continue?

Dim Thistextfile, fso, obj, things
Const ForReading = 1

Thistextfile = "C:\Listofthings.txt"
Set fso = CreateObject("Scripting.FileSystemObject")

Set obj = fso.OpenTextFile(Thistextfile,ForReading)

Do Until obj.AtEndOfStream
things = obj.ReadLine
things = Trim(things)
    If Len(things) > 0 Then
        WScript.Echo things
    End If
Loop

obj.close
bansi
  • 55,591
  • 6
  • 41
  • 52
Qin
  • 881
  • 7
  • 10

1 Answers1

1

You could collect each line into a dictionary or .Net's ArrayList, then write each line to the new file if it already exists in that collection:

Dim Thistextfile, fso, obj, things
Const ForReading = 1

Thistextfile = "C:\Listofthings.txt"
Set fso = CreateObject("Scripting.FileSystemObject")

Set obj = fso.OpenTextFile(Thistextfile,ForReading)

dim dictionary: set dictionary = CreateObject("Scripting.Dictionary")
dictionary.CompareMode = 1 'vbTextCompare
dim outputfile: set outputfile = fso.CreateTextFile("c:\duplicates.txt", true, true)

Do Until obj.AtEndOfStream
    things = obj.ReadLine()
    things = Trim(things)
    If Len(things) > 0 Then
        WScript.Echo things
    End If

    'if it already exists, it's a duplicate
    if dictionary.exists(things) then
        outputfile.writeline things
    else
        dictionary.add things, null
    end if
Loop

outputfile.close
obj.close
Community
  • 1
  • 1