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--
Quote:
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.
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.
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..
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:
Option Explicit
Private Sub Form_Load()
Dim str$, SaveStr$
Dim ff As Integer, ff2 As Integer
SaveStr = ""
ff = FreeFile
Open App.Path & "\InLog.txt" For Input As #ff
ff2 = FreeFile
Open App.Path & "\OutLog.txt" For Output As #ff2
Do While Not EOF(ff)
Line Input #ff, str
If SaveStr <> str Then
Print #ff2, str
SaveStr = str
End If
Loop
Close #ff2
Close #ff
End Sub
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:
Dim buffer As String
Dim lines() As String
Dim hFile As Long
hFile = FreeFile()
Open <filename> For Binary Access Read Lock Write As #hFile
buffer = Space$(LOF(hFile))
Get #hFile, 1, buffer
Close #hFile
lines = Split(buffer, vbNewLine)
Dim colLines As Dictionary
Dim i As Long
Set colLines = New Dictionary
For i = 0 To UBound(lines)
If (Not colLines.Exists(lines(i))) Then _
colLines.Add lines(i), vbNullString
Next i
Dim item As Variant
For Each item In colLines.Keys
Debug.Print CStr(item)
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" :)
Re: Removing multiple enteries in a text file
Easier for a ListBox:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim str$, SaveStr$
Dim ff As Integer
SaveStr = ""
list1.Clear
ff = FreeFile
Open App.Path & "\InLog.txt" For Input As #ff
Do While Not EOF(ff)
Line Input #ff, str
If SaveStr <> str Then
list1.AddItem str
SaveStr = str
End If
Loop
Close #ff
End Sub