I am trying to read through a text file and pull out bits of information I need. I have it 1/2 working but need to figure out how to loop through it to get the other 1/2

Sample log file
'--------------------------------------------------------------------------------------------------------------

[01:32:18.281] [@Flos'tok] [@Flos'tok] [Force Valor {875503313485824}] [ApplyEffect {836045448945477}: Force Valor {875503313485824}] ()
[01:32:18.281] [@Flos'tok] [@Flos'tok] [Force Valor {875503313485824}] [ApplyEffect {836045448945477}: Fortification {875503313486158}] ()
[01:32:18.282] [@Flos'tok] [@Flos'tok] [Force Valor {875503313485824}] [ApplyEffect {836045448945477}: Lucky Shots {875503313486145}] ()
[01:32:18.282] [@Flos'tok] [@Flos'tok] [Bio-Enhanced Resolve Stim {1483086567047168}] [ApplyEffect {836045448945477}: Bio-Enhanced Resolve Stim {1483086567047168}] ()
[01:32:18.282] [@Flos'tok] [@Flos'tok] [Safe Login {973870949466112}] [ApplyEffect {836045448945477}: Safe Login Immunity {973870949466372}] ()
[01:32:18.369] [@Flos'tok] [@Flos'tok] [] [Spend {836045448945473}: Force {836045448938502}] (100)
[01:32:18.369] [@Flos'tok] [@Flos'tok] [Combat Technique {979806594269184}] [ApplyEffect {836045448945477}: Combat Technique {979806594269184}] ()
[01:32:18.370] [@Flos'tok] [@Flos'tok] [Sprint {810670782152704}] [ApplyEffect {836045448945477}: Sprint {810670782152704}] ()
[01:32:19.967] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [ {2531161666486272}] [ApplyEffect {836045448945477}: Lucky Shots {2531161666486533}] ()
[01:32:19.967] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [ {2531161666486272}] [ApplyEffect {836045448945477}: Force Valor {2531161666486540}] ()
[01:32:19.968] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [ {2531161666486272}] [ApplyEffect {836045448945477}: Fortification {2531161666486543}] ()
[01:32:22.886] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [Med Scan {2074812801351680}] [Event {836045448945472}: AbilityActivate {836045448945479}] ()
[01:32:24.921] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [Med Scan {2074812801351680}] [ApplyEffect {836045448945477}: Heal {836045448945500}] (606) <45>
[01:32:26.030] [@Flos'tok] [@Flos'tok] [Safe Login {973870949466112}] [RemoveEffect {836045448945478}: Safe Login Immunity {973870949466372}] ()
[01:32:26.031] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [Safe Login {973870949466112}] [RemoveEffect {836045448945478}: Safe Login Immunity {973870949466372}] ()
[01:32:38.426] [@Flos'tok] [@Flos'tok] [Looting {810795336204288}] [Event {836045448945472}: AbilityActivate {836045448945479}] ()
[01:32:59.413] [@Flos'tok] [@Flos'tok] [Deploy Field Repair Droid {2941953813512192}] [Event {836045448945472}: AbilityActivate {836045448945479}] ()
[01:33:00.966] [@Flos'tok] [@Flos'tok] [Deploy Field Repair Droid {2941953813512192}] [ApplyEffect {836045448945477}: Basic Field Repair Droid {2941953813512452}] ()

'----------------------------------------------------------------------------------------------------------------

The code I am using to read a specific line in my log file

'----------------------------------------------------------------------------------------------------------------

Public Shared Function ReadSpecifiedLine(file As String, lineNum As Integer) As String
        'create a variable to hold the contents of the file
        Dim contents As String = String.Empty
        'always use a try...catch to deal
        'with any exceptions that may occur
        Try
            Dim logFileStream As New FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
            Using stream As New StreamReader(logFileStream)
                contents = stream.ReadToEnd().Replace(vbCr & vbLf, vbLf).Replace(vbLf & vbCr, vbLf)
                Dim linesArray As String() = contents.Split(New Char() {ControlChars.Lf})

                'Make sure we have ana ctual array
                If linesArray.Length > 1 Then
                    'Make sure user didnt provide number greater than the number
                    'of lines in the array, and not less than 0 (zero) Thanks AdamSpeight2008
                    If Not lineNum > linesArray.Length AndAlso Not lineNum < 0 Then
                        Return linesArray(lineNum)
                    Else
                        'Failed our check so return the first line in the array
                        Return linesArray(0)
                    End If
                Else
                    'No array so return the line
                    Return contents
                End If
            End Using
        Catch Ex As Exception
            Return Ex.ToString()
        End Try
    End Function

'----------------------------------------------------------------------------------------------------------------

And finally my code that 1/2 works

'----------------------------------------------------------------------------------------------------------------

 Public Function GetToonName(ByVal WhichToon)
        If WhichToon = 1 Then
            Dim ToonInfo As String = ReadSpecifiedLine(Label6.Text, 1)
            Dim str As String
            Dim strArr() As String
            Dim count As Integer
            str = ToonInfo
            strArr = str.Split(New Char() {"]"c})
            For count = 0 To strArr.Length - 1
                Try

                    Dim strValue
                    strValue = Replace(Replace(Regex.Replace(strArr(1), "\{.+?}]*?", ""), "@", ""), "[", "")
                    Label12.Text = strValue 'debug output
                    Label17.Text = Label12.Text
                    If Label7.Text = 0 Then
                        Label17.Text = "Loading"
                    End If
                Catch Ex As Exception
                    LogConsole.Text = Ex.Message
                End Try
            Next
        End If

        If WhichToon = 2 Then
            'this one is funky.
            'I need to check for a : and if it doesn't have one, read the next line, and if it doesn't have one the next  until it does
            'up to line 20 and then give up and list as no companion
            Dim ToonInfo As String = ReadSpecifiedLine(Label6.Text, 9 - 1)
            Dim str As String
            Dim strArr() As String
            Dim count As Integer
            str = ToonInfo
            strArr = str.Split(New Char() {"]"c})
            For count = 0 To strArr.Length - 1
                Try
                    LogConsole.Text = strArr(1)
                    Dim strValue
                    strValue = Replace(Replace(Regex.Replace(strArr(1), "\{.+?}]*?", ""), "@", ""), "[", "")
                    Label13.Text = Replace(strValue, Label12.Text + ":", "")  'debug output
                    Label16.Text = Label13.Text
                Catch Ex As Exception
                    LogConsole.Text = Ex.Message
                End Try
            Next
        End If
        ' - strAryWords is now an array

        Return 0
    End Function

'----------------------------------------------------------------------------------------------------------------

The 1st part "If WhichToon = 1 Then" works perfectly as the 1st line of code always has what I look for
thee 2nd part "If WhichToon = 2 Then" Doesn't
Right now I have it set to look at the 2nd line for toonname:companionname but it might be in the 3 or the 20th line [or anywhere in between, or not at all if there is no companion]

I've used things like If strArr(1).Contains(":") Then and it can see it [or not] depending on which line I manually tell it to look at and I have run it through a loop such as

IF NOT strArr(1).Contains(":") Then
Dim i 
For i = 0 to 20
ToonInfo = ReadSpecifiedLine(Label6.Text, i)
next

which does loop through it and I can see the output in my debug field but I can't "trap" it when it does find it.

Any ideas on how I might be able to do this in real basic methods?

Recommended Answers

All 3 Replies

I think you want something like this:

dim i as integer 

For i = 1 to 20
   ToonInfo = ReadSpecifiedLine(label6.Text, i)
   if Instr(ToonInfo, ":") <> 0  then
    'do what ever
    exit for
   end if
next

Instr searches for an instance of a string within a string if it finds the search string inside the string being searched it will return an integer value of the position within the search string in the string being searched. If not it will return a zero.
e.g.

Dim Works as string ="apple"
Dim Fruits as string  = "apple,pear,peach"
dim NotWork as string = "melon"
dim i, y as integer
i = Instr(Fruits, Works) 'i will equal 1
y = intr(Fruits, Works) 'y will equal 0 as "melon" is not in the Fruit string

I came up with a different method that seems to work ok. It isn't pretty but does get me what I needed to get

I ended up splitting it into 2 function

Public Function GetToonName()
        Dim ToonInfo As String = ReadSpecifiedLine(Label6.Text, 1)
        Dim str As String
        Dim strArr() As String
        Dim count As Integer
        str = ToonInfo
        strArr = str.Split(New Char() {"]"c})
        For count = 0 To strArr.Length - 1
            Try
                Dim strValue
                strValue = Replace(Replace(Replace(Regex.Replace(strArr(1), "\{.+?}]*?", ""), "@", ""), "[", ""), " ", "")
                If Label16.Text = "Loaded" Then
                    Label17.Text = strValue
                End If
            Catch Ex As Exception
                LogConsole.Text = LogConsole.Text & "It fucking errored " & Ex.Message
            End Try
        Next
        'Create Toons data dir
        Dim TLogDir As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & "Star Wars - The Old Republic\CombatLogs\ReaderFiles" & "\" & Label17.Text
        If Not My.Computer.FileSystem.DirectoryExists(TLogDir) Then
            My.Computer.FileSystem.CreateDirectory(TLogDir)
        End If

        Return 0
    End Function
    Public Function GetCompName()
        Dim strCompName As String = ""
        Dim logFileStream As New FileStream(Label6.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Try
            Using rdrFile As StreamReader = New StreamReader(logFileStream)
                Dim strCurrentLine As String = ""
                Do
                    strCurrentLine = rdrFile.ReadLine
                    'LogConsole.Text = LogConsole.Text & strCurrentLine
                    Dim TheCall = "@" & Label17.Text & ":"
                    'RichTextBox2.Text = TheCall
                    If strCurrentLine.Contains(TheCall) Then
                        'Store whole line into variable
                        strCompName = strCurrentLine

                        Exit Do
                    End If
                Loop Until strCurrentLine Is Nothing
            End Using 'rdrFile (closes file & disposes object)
        Catch Ex As Exception
            LogConsole.Text = Ex.Message
        End Try
        Dim TheCompLine = strCompName

        Dim str As String
        Dim strArr() As String
        Dim count As Integer
        str = TheCompLine
        strArr = str.Split(New Char() {"]"c})
        For count = 0 To strArr.Length - 1
            Try

                Dim strValue
                strValue = Replace(Replace(Replace(Regex.Replace(strArr(1), "\{.+?}]*?", ""), "@", ""), "[", ""), Label17.Text + ":", "")

                Label19.Text = strValue
            Catch Ex As Exception
                LogConsole.Text = Ex.Message
            End Try
        Next
        'Create Comp Data File
        Return 0
    End Function

like I said, it's not pretty :(
Instead of trying to loop 1 line at a time, I just read the whole file and get what I need that way

Tell me in English (not in code) what you want (not just the last part put the whole thing) and I'll see if we can come up with something simpler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.