Results 1 to 6 of 6

Thread: Removing multiple enteries in a text file

  1. #1

    Thread Starter
    Addicted Member Chrispybee's Avatar
    Join Date
    Sep 2003
    Location
    North Wales, UK
    Posts
    217

    Removing multiple enteries in a text file

    Hello, Hoping that some one could help me on this one. Below is part of a text file that I need to amend. It's a log file that an Application generates and would like to remove multiple enteries of per instance...

    For example:

    In Text file--
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147656 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147671 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147671 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147671 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147671 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147671 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T21296 106147734 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147734 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147734 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147734 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147734 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147734 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147734 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147750 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147750 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147750 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147750 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147750 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147750 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T21296 106147750 [CRulesChecker::LogProgress] File: wshom.ocx, User: IT2000\sshp21kd
    L3 T11100 106147781 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147781 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147781 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T11100 106147781 [CRulesChecker::LogProgress] File: SpeedScreenApi.dll, User: IT2000\ssnf34cm
    L3 T12112 106147812 [CRulesChecker::LogProgress] File: taskmgr.exe, User: IT2000\hitsdh
    L3 T12112 106147812 [CRulesChecker::LogProgress] File: taskmgr.exe, User: IT2000\hitsdh
    L3 T12112 106147812 [CRulesChecker::LogProgress] File: taskmgr.exe, User: IT2000\hitsdh
    L3 T12112 106147812 [CRulesChecker::LogProgress] File: taskmgr.exe, User: IT2000\hitsdh
    L3 T12112 106147812 [CRulesChecker::LogProgress] File: taskmgr.exe, User: IT2000\hitsdh
    So.. I'm trying to get 1 instance and put it either into another log file or in a list box. I've tried using strComp but failed.

    Any help on this would be great.

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Removing multiple enteries in a text file

    If you enter each line into a Collection and use the entry as a Key, then it will automatically not allow duplicates.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  3. #3

    Thread Starter
    Addicted Member Chrispybee's Avatar
    Join Date
    Sep 2003
    Location
    North Wales, UK
    Posts
    217

    Re: Removing multiple enteries in a text file

    Thanks for the reply. Can you shed some light on how to use collections and how I could do this. I'm not asking for the whole code but a starting point would be great..

    Thanks in advance..

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing multiple enteries in a text file

    Seems like overkill, to me. Just loop thru the file, and write the lines that are different.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim str$, SaveStr$
    5.   Dim ff As Integer, ff2 As Integer
    6.   SaveStr = ""
    7.   ff = FreeFile
    8.   Open App.Path & "\InLog.txt" For Input As #ff
    9.     ff2 = FreeFile
    10.     Open App.Path & "\OutLog.txt" For Output As #ff2
    11.       Do While Not EOF(ff)
    12.         Line Input #ff, str
    13.           If SaveStr <> str Then
    14.             Print #ff2, str
    15.             SaveStr = str
    16.           End If
    17.       Loop
    18.     Close #ff2
    19.   Close #ff
    20. End Sub

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing multiple enteries in a text file

    I suggest you use the Dictionary class, which has an "Exists" method that you can use to check whether a string is already present. Also it's a bit faster.

    VB Code:
    1. Dim buffer      As String
    2.     Dim lines()     As String
    3.     Dim hFile       As Long
    4.  
    5.     hFile = FreeFile()
    6.     Open <filename> For Binary Access Read Lock Write As #hFile
    7.     buffer = Space$(LOF(hFile))
    8.     Get #hFile, 1, buffer
    9.     Close #hFile
    10.    
    11.     lines = Split(buffer, vbNewLine)
    12.  
    13.     Dim colLines    As Dictionary
    14.     Dim i           As Long
    15.     Set colLines = New Dictionary
    16.     For i = 0 To UBound(lines)
    17.         If (Not colLines.Exists(lines(i))) Then _
    18.             colLines.Add lines(i), vbNullString
    19.     Next i
    20.  
    21.     Dim item As Variant
    22.     For Each item In colLines.Keys
    23.         Debug.Print CStr(item)
    24.     Next item

    that should get you started, if not do the whole thing anyway

    Edit: a bit late, but I forgot to mention you need a reference to the "Microsoft Scripting Runtime"
    Last edited by penagate; Nov 25th, 2005 at 11:45 PM.

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing multiple enteries in a text file

    Easier for a ListBox:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim str$, SaveStr$
    5.   Dim ff As Integer
    6.   SaveStr = ""
    7.   list1.Clear
    8.   ff = FreeFile
    9.   Open App.Path & "\InLog.txt" For Input As #ff
    10.     Do While Not EOF(ff)
    11.       Line Input #ff, str
    12.         If SaveStr <> str Then
    13.           list1.AddItem str
    14.           SaveStr = str
    15.         End If
    16.     Loop
    17.   Close #ff
    18. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width