Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Files in VB

I need to erase duplicate lines in a file.
please write the code that does it.
0
amird
Asked:
amird
  • 5
  • 3
  • 2
  • +1
1 Solution
 
amirdAuthor Commented:
Adjusted points to 100
0
 
ChasDevlinCommented:
What is your file?   Is it a database or a text file?
0
 
amirdAuthor Commented:
it's a text file

i need the function that search for duplicate lines and erase them

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MirkwoodCommented:
Are the duplicate lines after each other?

Sample:
xxx
xxx
yyy
zzz
zzz
xxx

So should the following file become a or b
a)
xxx
yyy
zzz
xxx
b)
xxx
yyy
zzz


0
 
amirdAuthor Commented:
yes and no

the duplicate line scan be after each other or somewhere in the file
0
 
MirkwoodCommented:
So option B is what you want? If not please give an example.
0
 
deightonCommented:
Private Sub RemoveDup(sFilePath As String)

    Dim iFile1 As Integer
    Dim iFile2 As Integer
    Dim iLine As Long
    Dim iline2 As Long
   
    Dim sString1 As String
    Dim sString2 As String
   
    Dim bDel(30000) As Boolean
   
    iFile1 = FreeFile
    Open sFilePath For Input As #iFile1
   
    While Not EOF(iFile1)
   
        Line Input #iFile1, sString1
        iLine = iLine + 1
   
        iFile2 = FreeFile
        Open sFilePath For Input As #iFile2
       
        iline2 = 0
        While Not EOF(iFile2)
       
            Line Input #iFile2, sString2
            iline2 = iline2 + 1
       
            If iline2 > iLine And sString1 = sString2 Then
           
                bDel(iline2) = True
               
            End If
           
        Wend
       
        Close iFile2
       
    Wend


    Close iFile1
   
    iFile1 = FreeFile
    Open sFilePath For Input As #iFile1
   
    iFile2 = FreeFile
   
    'Your text file is output here with no duplicates
    Open "Output" For Output As #iFile2
   
    iLine = 0
   
    While Not EOF(iFile1)
   
        Line Input #iFile1, sString1
        iLine = iLine + 1
        If Not bDel(iLine) Then
       
            Print #iFile2, sString1
           
        End If
       
    Wend
   
    Close #iFile1, #iFile2
   
   
End Sub


0
 
deightonCommented:
sFilePath is the name and path of your file to process e.g.

"c:\data\file1.txt"


0
 
amirdAuthor Commented:
the function doesnt work !!!!!!!!!!
0
 
amirdAuthor Commented:
Sorry Thank you its working great
0
 
deightonCommented:
ok, any more problems then just let me know.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now